Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with combobox

Status
Not open for further replies.

MangeInf

Programmer
Joined
Mar 30, 2005
Messages
1
Location
SE
Hi!
I have a problem with my combobox, it wont't display my recordset. The code works but i dosn't show the recordset in the combobox. What am I doing wrong ? suggestions?

The code:

Private Sub cmdSearch_Click()

Dim rstCustomer As ADODB.Recordset
Dim blnFound As Boolean

Dim fldItem As ADODB.Field

blnFound = False

If Me.txtName = "" Then Exit Sub

Set rstCusomer = New ADODB.Recordset
rstCustomer.Open "SELECT * FROM Customer WHERE Name = '" & _
txtName & "'", _
CurrentProject.Connection, _
adOpenStatic, adLockReadOnly, adCmdText

With rstCusomer
While Not .EOF
For Each fldItem In .Fields

If fldItem.Name = "Name" Then
If fldItem.Value = txtName Then
Me.comboBox1.AddItem .Fields("Id").Value
Me.comboBox1.AddItem .Fields("Name").Value
Me.comboBox1.AddItem .Fields("Address")
Me.comboBox1.AddItem .Fields("Zipcode")
Me.comboBox1.AddItem .Fields("Telefon")
Me.comboBox1.AddItem .Fields("Email")
Me.comboBox1.AddItem .Fields("Notes")
blnFound = True
End If
End If
Next
.MoveNext
Wend
End With

' If the item number was not found, ...
If blnFound = False Then
' ... let the user know, ...
MsgBox "Customer dosn't exist"
' ... and reset the form
cmdReset_Click
End If

rstCustomer.Close
Set rstCustomer = Nothing
End Sub
 
You cannot use AddItem with VBA. Create a Value List for the Row Source or set the Row Source to the name of a table, query, or SQL string.
 
Here try this...

Private Sub cmdSearch_Click()

Me.comboBox1.RowSource = "SELECT Name, Address, Zipcode, Telefon, Email, Notes FROM Customer WHERE Name = '" & Me.txtName & "'"

Me.comboBox1.requery

if Me.comboBox1.ListCount =0 then
' ... let the user know, ...
MsgBox "Customer dosn't exist"
' ... and reset the form
cmdReset_Click
endif

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top