DLookup Pulling Wrong Record

xljunkie

Board Regular
Joined
May 20, 2011
Messages
92
Ok so I have a table below which lists out various text strings associated with months. A user selects a month bound to the first column from ComboBoxMonth.

MonthNumberMonthNameImportMonthImportDate
02January0131
03February0228
04March0331
05April0430
06May0531
07June0630
08July0731
09August0831
10September0930
11October1031
12November1130
01December1231

<caption> tblMonths </caption> <thead>
</thead> <tbody>
</tbody> <tfoot></tfoot>


In my VBA, I have the following:

Code:
    ImportMonth = DLookup("ImportMonth", "tblMonths", Me.ComboBoxMonth)
    ImportDate = DLookup("ImportDate", "tblMonths", Me.ComboBoxMonth)

As an example, I've chosen March in my ComboBoxMonth, and that returns a value of "04". When I run the code, the value of ImportMonth = "01" and the value of ImportDate = "31" while the value of Me.ComboBoxMonth is "04". Is there something I'm missing?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi,
Be sure you know what the bound column in the combobox is (the real "value" of the combobox depends on what field it is bound to, not what it displays visibly).
 
Upvote 0
It is bound to MonthNumber so "01", "02", "03", etc. The display is MonthName. I will try to figure it out next week.
 
Upvote 0
Fixed it. I didn't know you needed this complicated syntax for a form object.

Code:
    ImportMonth = DLookup("ImportMonth", "tblMonths", "MonthNumber = '" & Forms!FrmFrontPage!ComboBoxMonth & "'")
    ImportDate = DLookup("ImportDate", "tblMonths", "MonthNumber = '" & Forms!FrmFrontPage!ComboBoxMonth & "'")
 
Upvote 0
You would not need that syntax if the dlookup formula is in the same form. That's what Me means in your original post. Me is self-referential to the form object.
 
Upvote 0

Forum statistics

Threads
1,221,877
Messages
6,162,583
Members
451,776
Latest member
bosvinn

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