Hi. I have a listbox with four fields in a form: User_First_Name, User_Last_Name, Phone_No, and Product_ID. I am trying to create command buttons for each field that, when clicked, will reorder the listbox by that field. So, I made four buttons in the form and tried to configure them accordingly.
Before adding the command buttons, when an entry was highlighted in the listbox (named QuickList), it populated several text box fields on the other side of the form. After the button to reorder the form is clicked, however, clicking on one of the entries in the listbox results in the following error:
"Run-time error '13': Type mismatch"
In addition, clicking on the FIRST NAME, LAST NAME, and PHONE NO buttons all make the User_First_Name and Product_ID fields in the listbox disappear. The PHONE NO button will reorder the listbox by phone number, but both the FIRST NAME and LAST NAME buttons reorder the listbox by last name. Clicking the PRODUCT ID button makes all four fields disappear.
Here is the code for the form.
I'm quite confused. Can anyone help me to get these buttons to work? Thanks!
Before adding the command buttons, when an entry was highlighted in the listbox (named QuickList), it populated several text box fields on the other side of the form. After the button to reorder the form is clicked, however, clicking on one of the entries in the listbox results in the following error:
"Run-time error '13': Type mismatch"
In addition, clicking on the FIRST NAME, LAST NAME, and PHONE NO buttons all make the User_First_Name and Product_ID fields in the listbox disappear. The PHONE NO button will reorder the listbox by phone number, but both the FIRST NAME and LAST NAME buttons reorder the listbox by last name. Clicking the PRODUCT ID button makes all four fields disappear.
Here is the code for the form.
Code:
Option Compare Database
Option Explicit
'* Define the RowSource for the listbox, along with different sorted variations
Const mcRowSourceBasis = "SELECT DISTINCTROW User_First_Name, User_Last_Name, Phone_No FROM [Main]"
Const mcRowSourceUnsorted = mcRowSourceBasis & ";"
Const mcRowSourceSortCol1 = mcRowSourceBasis & " Order By 1;"
Const mcRowSourceSortCol1Desc = mcRowSourceBasis & " Order By 1 Desc;"
Const mcRowSourceSortCol2 = mcRowSourceBasis & " Order By 2;"
Const mcRowSourceSortCol2Desc = mcRowSourceBasis & " Order By 2 Desc;"
Const mcRowSourceSortCol3 = mcRowSourceBasis & " Order By 3;"
Const mcRowSourceSortCol3Desc = mcRowSourceBasis & " Order By 3 Desc;"
Const mcRowSourceSortCol4 = mcRowSourceBasis & " Order By 4;"
Const mcRowSourceSortCol4Desc = mcRowSourceBasis & " Order By 4 Desc;"
Private Sub Command27_Click()
On Error GoTo Err_Command27_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "History"
stLinkCriteria = "[System_ID]=" & "'" & Me![System_ID] & "'"
DoCmd.OpenForm stDocName, , , stLinkCriteria
History.Visible = False
Exit_Command27_Click:
Exit Sub
Err_Command27_Click:
MsgBox Err.Description
Resume Exit_Command27_Click
End Sub
Private Sub Account_AfterUpdate()
Me.Account_Desc = Me.Account.Column(0)
End Sub
Private Sub cboCarrier_AfterUpdate()
If MsgBox("Changes have been made to this record." _
& vbCrLf & vbCrLf & "Do you want to save these changes?" _
, vbYesNo, "Changes Made...") = vbYes Then
Me.cboProduct_ID = Null
Me.cboProduct_ID.Requery
Me.cboProduct_ID = Me.cboProduct_ID.ItemData(0)
Me.Account.Requery
Me.Account = Me.Account.ItemData(0)
DoCmd.Save
Else
DoCmd.RunCommand acCmdUndo
End If
End Sub
Private Sub Command28_Click()
On Error GoTo Err_Command28_Click
History.Visible = True
Exit_Command28_Click:
Exit Sub
Err_Command28_Click:
MsgBox Err.Description
Resume Exit_Command28_Click
End Sub
Private Sub Command34_Click()
History.Visible = False
End Sub
Private Sub Check136_Click()
Cancelled_Date = Now()
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
'TimeStamp = Now()
End Sub
Private Sub List72_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[ID] = " & Str(Nz(Me![List72], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
Private Sub Form_Load()
Me.Caption = "Postle Mobile Communication Database"
End Sub
Private Sub QuickList_AfterUpdate()
' Find the record that matches the control.
Dim rs As Object
Set rs = Me.Recordset.Clone
rs.FindFirst "[System_ID] = " & Str(Nz(Me![QuickList], 0))
If Not rs.EOF Then Me.Bookmark = rs.Bookmark
End Sub
Private Sub Toggle_SubForm_Click()
History.Visible = True
If History.Visible = True Then
o
End Sub
Private Sub Billing_Subform_Button_Click()
On Error GoTo Err_Billing_Subform_Button_Click
Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "Billing subform"
stLinkCriteria = "[Parent_ID]=" & Me![System_ID]
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Billing_Subform_Button_Click:
Exit Sub
Err_Billing_Subform_Button_Click:
MsgBox Err.Description
Resume Exit_Billing_Subform_Button_Click
End Sub
Private Sub cmdSortCol1_Click()
'* Set the listbox RowSource to a pre-defined SQL statement stored in a module constant.
'* See the form module declarations section for the module constants' definition.
If QuickList.RowSource = mcRowSourceSortCol1 Then
'* Sort records in descending order
QuickList.RowSource = mcRowSourceSortCol1Desc
Else
'* Sort records in ascending order
QuickList.RowSource = mcRowSourceSortCol1
End If
End Sub
Private Sub cmdSortCol2_Click()
'* Set the listbox RowSource to a pre-defined SQL statement stored in a module constant.
'* See the form module declarations section for the module constants' definition.
If QuickList.RowSource = mcRowSourceSortCol2 Then
'* Sort records in descending order
QuickList.RowSource = mcRowSourceSortCol2Desc
Else
'* Sort records in ascending order
QuickList.RowSource = mcRowSourceSortCol2
End If
End Sub
Private Sub cmdSortCol3_Click()
'* Set the listbox RowSource to a pre-defined SQL statement stored in a module constant.
'* See the form module declarations section for the module constants' definition.
If QuickList.RowSource = mcRowSourceSortCol3 Then
'* Sort records in descending order
QuickList.RowSource = mcRowSourceSortCol3Desc
Else
'* Sort records in ascending order
QuickList.RowSource = mcRowSourceSortCol3
End If
End Sub
Private Sub cmdSortCol4_Click()
'* Set the listbox RowSource to a pre-defined SQL statement stored in a module constant.
'* See the form module declarations section for the module constants' definition.
If QuickList.RowSource = mcRowSourceSortCol4 Then
'* Sort records in descending order
QuickList.RowSource = mcRowSourceSortCol4Desc
Else
'* Sort records in ascending order
QuickList.RowSource = mcRowSourceSortCol4
End If
End Sub
I'm quite confused. Can anyone help me to get these buttons to work? Thanks!