Mike
Mike

Reputation: 11

Updating text box value from combobox option

I can't work out where I am going wrong with my code. When the user selects a value in the combo box, i want it to go to the Consultants table and grab the default rate for that consultant and stick it in the Hourly Rate text box. This is the msg that I get when I update the combobox.

Private Sub cmbConsultant_Change()
Dim db As Database
Dim rs As DAO.Recordset ''Requires reference to Microsoft DAO x.x Library
Dim strSQL As String

strSQL = "defaultFee * FROM tblConsultants WHERE ID = """ & Me!cmbConsultant & """"

Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL)

If rs.RecordCount > 0 Then
   Me.txtHourlyRate = rs!CountOfEndDeviceType
Else
   Me.txtHourlyRate = ""
End If

Set rs = Nothing
Set db = Nothing

End Sub

Upvotes: 0

Views: 1038

Answers (2)

Gustav
Gustav

Reputation: 55806

You could use DLookup for this - much simpler:

Private Sub cmbConsultant_Change()

    Me!txtHourlyRate.Value = DLookup("defaultFee", "tblConsultants", "ID = '" & Me!cmbConsultant.Value & "'")

End Sub

However, most likely your ID is numeric, thus:

Private Sub cmbConsultant_Change()

    Me!txtHourlyRate.Value = DLookup("defaultFee", "tblConsultants", "ID = " & Me!cmbConsultant.Value & "")

End Sub

Upvotes: 2

Tedo G.
Tedo G.

Reputation: 1565

I Think You need some SELECT Here

strSQL = "defaultFee * FROM tblConsultants WHERE ID = """ & Me!cmbConsultant & """"

It Should Be:

strSQL = "SELECT defaultFee AS [CountOfEndDeviceType] FROM tblConsultants WHERE ID = """ & Me!cmbConsultant & """"

Also Note, That [CountOfEndDeviceType] Must be a FieldName, SO I've put it in a Select statement.

Upvotes: 1

Related Questions