Entering result of a query in a textbox using VBA

Nevy

Registered User.
Local time
Yesterday, 18:48
Joined
May 31, 2005
Messages
31
I'm pretty sure there's a simple solution to this, yet I can't seem to find it.

I have a table that contains the Months of the year with an MonthID:
1|January
2|Febuary
...

I also have a form that contains a combobox with the Months.

When the user chooses the month, a query gets created, returning the MonthID.

I want to store the MonthID, in an invisible textbox, so that I can use it for my calculations.

If I use a textbox, I can't seem to display the result and if I use a listbox, I can't access that value.

How would I make it work?

This is the code used on the combobox:
Code:
Private Sub cbToMonth_Change() 
    Dim db As DAO.Database
    Dim qdf As DAO.QueryDef
    Dim strSQL As String
    
    Set db = CurrentDb()
    Set qdf = db.QueryDefs("getMonthID")

    strSQL = "SELECT Months1.MonthID " & _
             "FROM Months1 " & _
             "WHERE (((Months1.Month) In ('" & Me.cbToMonth.Value & "')));"
             
    qdf.SQL = strSQL
    Me.monthIDTo.Value = "getMonthID" 
    Set db = Nothing
    Set qdf = Nothing
End Sub
 
Last edited:
if your combo box is based on a query that includes the ID field, and the bound column of the combo box contains the ID field-data, then any time you refer to the combo box you are in essence refering to the ID. if you have selected March, then when you refer to the combo box you are refering to '3', not March. What you see is irrelevant; it is the bound column that is relevant for referral and calculation purposes.
 
Last edited:
And if that doesn't help, lookup DLookup from the Help Files as a way to grab an item from a table via a quickie lookup.
 
Great, it worked using the bound column.
Thanks.
 

Users who are viewing this thread

Back
Top Bottom