Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have access DB and few queries there.

Recently I have added an option group with two radio buttons(values: 1 and 2) to my form.

What I try to achieve is: when first radio button is checked queries should return data only for countries; when second one is checked queries should return only data for cities.

I created some code:

Private Sub Command54_Click()

Dim dtps As String

If Frame45.Value = 1 Then
    dtps = "101,103,1104,1105"  
Else
    dtps = "105,125,127,129,131,133,145,147,149,151,153,171,173,175,177,179,181,1105,1125,1127,1129,1131,1133,1141,1145,1147,1149,1151,1153,104,124,126,128,130,132,144,146,148,150,152,170,172,172,176,178,180,1104,1124,1126,1128,1130,1132,1144,1146,1146,1148,1150,1152"
End If


DoCmd.OpenQuery "test1", acViewNormal, acEdit

End Sub

Right now query "test1" is very simple:

"Select * from MyTable"

And my idea is to change it to :

  "Select * from MyTable Where CountryCodeID IN  ( @dtps )"

Does anybody know how to do it?


What I also tried is to use function:

My Query code:

Select * from MyTable Where CountryCodeID IN  ( getcountrycode() )

Function code is:

Private Sub Command54_Click()

'MsgBox Frame45.Value

DoCmd.OpenQuery "test1", acViewNormal, acEdit

End Sub

Public Function getcountrycode()

Dim dtps As String

If Frame45.Value = 1 Then
    dtps = "101,103,1104,1105"
Else
    dtps = "101,103,105,125,127,129,131,133,145,147,149,151,153,171,173,175,177,179,181,1105,1125,1127,1129,1131,1133,1141,1145,1147,1149,1151,1153,104,124,126,128,130,132,144,146,148,150,152,170,172,172,176,178,180,1104,1124,1126,1128,1130,1132,1144,1146,1146,1148,1150,1152"
End If

getcountrycode = dtps

End Function

It returns error: "Undefined function 'getcountrycode' in expression

share|improve this question

1 Answer 1

It would probably be easier to simply build the query:

sSQL = "SELECT * FROM MyTable Where CountryCodeID IN ( " & dtps & " )"

If Not IsNull(DLookup( _
    "Test1", "MSysObjects", "[Name]='Test1' AND Type= 5")) Then
    ''Query exists, overwrite the sql permanently
    CurrentDb.QueryDefs("Test1").SQL = sSQL
Else
    ''Note that you cannot have a query with the same name as a table, 
    ''so you might need to check that, too.
    CurrentDb.CreateQueryDef "Test1", sSQL
End If

DoCmd.OpenQuery "test1", acViewNormal, acEdit

Rather than simply opening a query, I strongly suggest you bind the query to a form.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.