Hi,
This may need to go in the queries section but does involve a bit of both.
Basically I am making a query which needs the criteria to be based on information in another table. Note this is the only filter on the column and the reason for the function is this information will vary over time and to save time later on when we add or remove criteria.
The query exists of a field called 'Depots' which contains depot numbers and will have a large number of records.
I need this to be filter by the list of depot numbers within a separate table so I build a function to run through the recordset and create the criteria, however if the depots field is set to number I get an error message 'Data Mismatch' (Hate this error) and as text I just get no Results.
Here the code for the function
Function CDepots()
Dim rs As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim strsql As String
Dim i As Integer
Dim DepotList As String
Set rs = New ADODB.Recordset
Set cnn = CurrentProject.Connection
i = 0
strsql = "SELECT [Concerto Depots].* " & _
"FROM [Concerto Depots];"
rs.Open strsql, cnn, adOpenDynamic, adLockReadOnly
rs.MoveFirst
Do Until rs.BOF = True Or rs.EOF = True
If i = 0 Then
DepotList = "" & rs.Fields("Depot Number") & ""
rs.MoveNext
Else
DepotList = DepotList & " Or " & rs.Fields("Depot Number") & ""
rs.MoveNext
End If
i = 1
Loop
Set rs = Nothing
Set cnn = Nothing
ConcertoDepots = DepotList
Debug.Print CDepots
The output is:
20 Or 30 Or 41 Or 42 Or 45 Or 52
I have tried this with ' around the numbers and ". either way I get the same results even if I change the field type
First issue I ever had with number fields in a long time.
Many thanks
This may need to go in the queries section but does involve a bit of both.
Basically I am making a query which needs the criteria to be based on information in another table. Note this is the only filter on the column and the reason for the function is this information will vary over time and to save time later on when we add or remove criteria.
The query exists of a field called 'Depots' which contains depot numbers and will have a large number of records.
I need this to be filter by the list of depot numbers within a separate table so I build a function to run through the recordset and create the criteria, however if the depots field is set to number I get an error message 'Data Mismatch' (Hate this error) and as text I just get no Results.
Here the code for the function
Function CDepots()
Dim rs As ADODB.Recordset
Dim cnn As ADODB.Connection
Dim strsql As String
Dim i As Integer
Dim DepotList As String
Set rs = New ADODB.Recordset
Set cnn = CurrentProject.Connection
i = 0
strsql = "SELECT [Concerto Depots].* " & _
"FROM [Concerto Depots];"
rs.Open strsql, cnn, adOpenDynamic, adLockReadOnly
rs.MoveFirst
Do Until rs.BOF = True Or rs.EOF = True
If i = 0 Then
DepotList = "" & rs.Fields("Depot Number") & ""
rs.MoveNext
Else
DepotList = DepotList & " Or " & rs.Fields("Depot Number") & ""
rs.MoveNext
End If
i = 1
Loop
Set rs = Nothing
Set cnn = Nothing
ConcertoDepots = DepotList
Debug.Print CDepots
The output is:
20 Or 30 Or 41 Or 42 Or 45 Or 52
I have tried this with ' around the numbers and ". either way I get the same results even if I change the field type
First issue I ever had with number fields in a long time.
Many thanks