MS Access code - Combo box code

sock

New Member
Joined
Mar 28, 2012
Messages
32
Hi,

Someone please help on the below code.. I am new to MS Access..

Code:
Option Compare Database
Private Sub Combo2_AfterUpdate()
Dim Org As String
    Org = "Select * from StoreDat where ([Customer Number] = " & Me.Combo2 & ")"
    Me.StoreDat_subform.Form.RecordSource = Org
    Me.StoreDat_subform.Form.Requery
End Sub



when I test this code it showing a "Data type mismatch in criteria expression" Error

The customer Number is Numbers
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If you open up the StoreDat table, and go to Design View, what is the Data Type of the Customer Number field?
 
Upvote 0
if you are selecting with raw sql (which is what you are doing when you write a sql command text), then you need to put text in single quotes.
Code:
"Select * from StoreDat where ([Customer Number] = " & chr(39) & Me.Combo2 & chr(39) & ")"
Or
Code:
"Select * from StoreDat where ([Customer Number] = '" & Me.Combo2 & "')"
 
Last edited:
Upvote 0
Sorry, not sure how I missed your reply earlier.
it is "Short text"
Yep, then it is text and needs to have text qualifiers (of single or double-quotes) around it, like xenou showed.
 
Last edited:
Upvote 0
Yes. it works.. here is the complete code.

Code:
Private Sub Combo2_AfterUpdate()
Dim Org As Variant
    Org = "Select * from StoreDat where ([Customer Number] = '" & Me.Combo2.Column(1) & "')"
    Me.StoreDat_subform.Form.RecordSource = Org
    Me.StoreDat_subform.Form.Requery
End Sub

Thank for your help...


regards
Sock

Me.StoreDat_subform.Form.RecordSource = Org
Me.StoreDat_subform.Form.Requery
End Sub
 
Upvote 0
Hi,

Someone please help on the below code.. I am new to MS Access..

Code:
Option Compare Database
Private Sub Combo2_AfterUpdate()
Dim Org As String
    Org = "Select * from StoreDat where ([Customer Number] = " & Me.Combo2 & ")"
    Me.StoreDat_subform.Form.RecordSource = Org
    Me.StoreDat_subform.Form.Requery
End Sub



when I test this code it showing a "Data type mismatch in criteria expression" Error

The customer Number is Numbers

TIP: Use Option Explicit


You an also bind the sub form's master and child field properties on the data tab. The master will be the Combo2 and the child [Customer Number]

Then the code would be:

Code:
Option Compare Database
Option Explicit

Private Sub Combo2_AfterUpdate()
    Me.StoreDat_subform.Form.Requery
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,669
Members
448,977
Latest member
moonlight6

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top