SQL query question [Resolved]
I have a db with a table called Customers. There are two columns in Customers: CustomerCode and CustomerName.
My vb form has a combo box with a list of the customer code, a text box, and a button.
What I want to do is: If a user chooses a customer code from the combo box I want the customer name to show in the text box.
This is the SQL query that I have been using:
strSQL = "SELECT CustomerName FROM Customers WHERE CustomerCode = '" & cboCustomerCode.Text & "'"
The problem i'm having is that the result of the customer name will not show on the text box.
This is what I used to put the result in the text box:
txtCustomerName = strSQL
Is this correct?
AutoFill ComboBox Function
This is what I use for a Combo AutoFill:
In A Module:
VB Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Public Function AUTOFIND(ByRef cboCurrent As ComboBox, _
ByVal KeyAscii As Integer, Optional ByVal LimitToList As Boolean = False)
Dim lCB As Long
Dim sFindString As String
If KeyAscii = 8 Then
If cboCurrent.SelStart <= 1 Then
cboCurrent = ""
AUTOFIND = 0
Exit Function
End If
If cboCurrent.SelLength = 0 Then
sFindString = UCase(Left(cboCurrent, Len(cboCurrent) - 1))
Else
sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart - 1)
End If
ElseIf KeyAscii < 32 Or KeyAscii > 127 Then
Exit Function
Else
If cboCurrent.SelLength = 0 Then
sFindString = UCase(cboCurrent.Text & Chr$(KeyAscii))
Else
sFindString = Left$(cboCurrent.Text, cboCurrent.SelStart) & Chr$(KeyAscii)
End If
End If
lCB = SendMessage(cboCurrent.hWnd, CB_FINDSTRING, -1, ByVal sFindString)
If lCB <> CB_ERR Then
cboCurrent.ListIndex = lCB
cboCurrent.SelStart = Len(sFindString)
cboCurrent.SelLength = Len(cboCurrent.Text) - cboCurrent.SelStart
AUTOFIND = 0
Else
If LimitToList = True Then
AUTOFIND = 0
Else
AUTOFIND = KeyAscii
End If
End If
End Function
On the Form:
VB Code:
Private Sub Combo1_KeyPress(KeyAscii As Integer)
On Error GoTo Combo1_KeyPress_Error
KeyAscii = AUTOFIND(Me.Combo1, KeyAscii, True)
On Error GoTo 0
Exit Sub
Combo1_KeyPress_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
"in procedure Combo1_KeyPress of Form " & Me.Name
End Sub