Solved

Passing a TempVar to a Combo box form

Posted on 2016-10-18
3
147 Views
Last Modified: 2016-10-19
In MS Access 2010 I am opening a single record form from another form with a button and passing a filter for the form with the button. That works fine. I also have a Combo box on the target form that I would like to populate with all the target forms filtered record set keys in order to allow the user to "jump" to the next record of choice rather than have to review the records sequentially.

So I am sending the Combo box query, select criteria as a TempVar. The Combo box query has a where clause, WHERE column IN ( [TempVars]![ComboFilterVariable] ). I pass this value, '("Active","On-Hold")' in one of the first forms SetTempVar to the target form.

When I replace the TempVar in the where clause with the value in TempVar which is ("Active","On-Hold"), assume the single quotes are removed by Access, the query works. But the query is not resolving the TempVar correctly as I get no rows using the TempVar in the query.

If I hard code the values the combo box it works. So not a combo box issue. I am using the same form with multiple filters so would really like to dynamically create the Combo box list to match the records provided by the filtered form rather than have multiple forms to display the same data for various lists.

Any help would be welcome, spent weeks on this on and off already.  
I am a beginner at macros and novice at MS Access.
0
Comment
Question by:Dave Harrison
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 38

Accepted Solution

by:
PatHartman earned 500 total points
ID: 41848843
The In() list is not something that can be dynamically changed.  You have to create the list and concatenate it into the SQL String and then pass the completed string as an argument.

So Where Somefield In(Tempvars!somefield)

actually needs to be passed as:

Where Somefield In("Active","On-Hold")

This is code in a form that builds the necessary SQL string and uses it to replace the RecordSource.  You can use it to replace the RowSource of your combo.
Private Sub cmdFilter_Click()
On Error GoTo Err_cmdFilter_Click
    Dim i As Variant
    Dim strIN As Variant
    Dim strSQL As String
    
    strIN = ""
    For Each i In lstSelect.ItemsSelected
        strIN = strIN & lstSelect.ItemData(i) & ","
    Next i
    strIN = Left(strIN, Len(strIN) - 1) ' remove trailing comma.
    strSQL = "Select * From AccessAndJetErrors Where ErrorCode IN(" & strIN & ");"
    Me.RecordSource = strSQL

Exit_cmdFilter_Click:
    Exit Sub

Err_cmdFilter_Click:
Select Case Err.Number
    Case 5
        MsgBox "Please select 1 or more items from the listbox", vbOKOnly
    Case Else
        MsgBox Err.Number & "-" & Err.Description
End Select
Resume Exit_cmdFilter_Click
    
End Sub

Open in new window


You can actually build the SQL string this way:

strSQL = "Select fld1, fld2, From YourTable Where Somefield In(" & Tempvars!somefield & ");"  The point is that VBA needs to resolve the Tempvar in this case since it is a string of strings rather than a simple string.
1
 

Author Closing Comment

by:Dave Harrison
ID: 41850306
Thank you so much
0
 
LVL 38

Expert Comment

by:PatHartman
ID: 41850467
You're welcome.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question