I have a simple MS Access form that has 3 objects: a text box, a list box, and a button. The intended use of the form is as follows: the user enters a name in the text box, selects an item from the list, then clicks the button to add data to the table.
However, when I click on the button, I continue to receive the error message: "You cannot reference the property or method for the control, unless the control has focus."
Below is my code. Thanks!
Private Sub addRecord_button_Click() Dim CustomerName As String Dim CustomerType As String On Error GoTo Errhandler CustomerName = "[name not selected]" CustomerType = "[type not selected]" CustomerName = Customer_TextBox.Text Select Case Type_ListBox.ListIndex Case 0 CustomerType = "Type 1" Case 1 CustomerType = "Type 2" Case 2 CustomerType = "Type 3" End Select 'MsgBox ("Name: " & CustomerName & " and Type: " & CustomerType) DoCmd.RunSQL "INSERT INTO Customer VALUES (CustomerName, CustomerType);" Errhandler: MsgBox ("The following error has occured: " & Err & " - " & Error(Err)) End Sub
source share