Code for different field types

G

Guest

I populate a combo box with a field list, which, when selected will populate
a list box using this code:
Dim SSQL As String

SSQL = "SELECT ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] FROM
ARCHIVED_ISSUES GROUP BY ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] "

Me.List67.RowSource = SSQL

That part works fine, but I get errors when I try to build a list with the
different selections from the multi-select list box, since the values could
be strings, numbers, dates. So, how do I make this:

separ = "'"

createlist:
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

flexible enough to be able to change the separ constant to be "'", "", or "#".

Sean
 
G

Guest

But how do I identify the field type for sfld or me.grpselect?
--
Ficticiously Yours, Biggles


mscertified said:
Put the logic in a function and pass the separator as a parameter.

Dorian

Biggles said:
I populate a combo box with a field list, which, when selected will populate
a list box using this code:
Dim SSQL As String

SSQL = "SELECT ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] FROM
ARCHIVED_ISSUES GROUP BY ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] "

Me.List67.RowSource = SSQL

That part works fine, but I get errors when I try to build a list with the
different selections from the multi-select list box, since the values could
be strings, numbers, dates. So, how do I make this:

separ = "'"

createlist:
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

flexible enough to be able to change the separ constant to be "'", "", or "#".

Sean
 
G

Guest

Check out the VarType() function.

This is air code, but it might help...

' I added an 's' here
ssepar = "'"
dsepar = "#"
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected

Select Case VarType(List67.ItemData(varnumber))
'for dates
Case vbDate ' constant = 7
sfilt = sfilt & dsepar & List67.ItemData(varnumber) & dsepar & ","
'for strings
Case vbString 'constant = 8
sfilt = sfilt & ssepar & List67.ItemData(varnumber) & ssepar & ","
'for numbers
Case vbInteger - vbCurrency, vbDecimal 'constants 2-6 & 14
sfilt = sfilt & List67.ItemData(varnumber) & ","
End Select
Next


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
But how do I identify the field type for sfld or me.grpselect?
--
Ficticiously Yours, Biggles


mscertified said:
Put the logic in a function and pass the separator as a parameter.

Dorian

Biggles said:
I populate a combo box with a field list, which, when selected will populate
a list box using this code:
Dim SSQL As String

SSQL = "SELECT ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] FROM
ARCHIVED_ISSUES GROUP BY ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] "

Me.List67.RowSource = SSQL

That part works fine, but I get errors when I try to build a list with the
different selections from the multi-select list box, since the values could
be strings, numbers, dates. So, how do I make this:

separ = "'"

createlist:
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

flexible enough to be able to change the separ constant to be "'", "", or "#".

Sean
 
G

Guest

Steve

That is what I was looking for, but every time I run the code:

Select Case VarType(List67.ItemData(varnumber))
Evaluates as a string. The entire code is below. Is there some other way I
should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer

sfld = Me.GRPSELECT

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.openreport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
Debug.Print VarType(List67.ItemData(varnumber))
Select Case VarType(List67.ItemData(varnumber))
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal
separ = ""
Case Else
Debug.Print VarType(List67.ItemData(varnumber))
End Select
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

--
Ficticiously Yours, Biggles


Steve Sanford said:
Check out the VarType() function.

This is air code, but it might help...

' I added an 's' here
ssepar = "'"
dsepar = "#"
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected

Select Case VarType(List67.ItemData(varnumber))
'for dates
Case vbDate ' constant = 7
sfilt = sfilt & dsepar & List67.ItemData(varnumber) & dsepar & ","
'for strings
Case vbString 'constant = 8
sfilt = sfilt & ssepar & List67.ItemData(varnumber) & ssepar & ","
'for numbers
Case vbInteger - vbCurrency, vbDecimal 'constants 2-6 & 14
sfilt = sfilt & List67.ItemData(varnumber) & ","
End Select
Next


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
But how do I identify the field type for sfld or me.grpselect?
--
Ficticiously Yours, Biggles


mscertified said:
Put the logic in a function and pass the separator as a parameter.

Dorian

:

I populate a combo box with a field list, which, when selected will populate
a list box using this code:
Dim SSQL As String

SSQL = "SELECT ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] FROM
ARCHIVED_ISSUES GROUP BY ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] "

Me.List67.RowSource = SSQL

That part works fine, but I get errors when I try to build a list with the
different selections from the multi-select list box, since the values could
be strings, numbers, dates. So, how do I make this:

separ = "'"

createlist:
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

flexible enough to be able to change the separ constant to be "'", "", or "#".

Sean
 
G

Guest

My bad, Biggles. I didn't think it through.

OK, so you have a combo box where you pick a field, which populates a list
box with the data from one field. The data can/might change each time you run
the report. One time you might be searching for records for certain dates,
other times it might be for certain text in the field.

So the trick is determining the field type using the field name. This is
done using a function "TableInfo()" passing two arguments: the table name and
the field name.
Below is the code:

' Select Case VarType(List67.ItemData(varnumber))
' Evaluates as a string. The entire code is below. Is there some
other way I
' should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer
Dim FldType As Long '*****

sfld = Me.GRPSELECT
FldType = Null

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.OpenReport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'determine the delimiter needed
FldType = TableInfo("ARCHIVED_ISSUES", sfld)
Select Case FldType
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal, vbSingle, vbDouble
separ = ""
Case Else
Debug.Print FldType
End Select

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
'Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
' lngLen = Len(sfilt) - 1
' If lngLen > 0 Then
If Len(sfilt) > 0 Then
sfilt = "[" & sfld & "] in (" & Left(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.OpenReport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

' I think this is where you would have an
'END SUB

'========================================

'the following code (modified) was shamelessly stolen from
'Allen Browne at http://allenbrowne.com/func-06.html

Function TableInfo(strTableName As String, strFieldName As String) As Long
On Error GoTo TableInfoErr
' Purpose: Determine the field type in a table
' Arguments: Name of a table in the current database, Name of a field in
the table
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Set db = CurrentDb()
Set tdf = db.TableDefs(strTableName)

For Each fld In tdf.Fields
If fld.Name = strFieldName Then
TableInfo = CLng(fld.Type) 'fld.Type is Integer, but constants are
Long.
End If
Next

TableInfoExit:
Set db = Nothing
Exit Function

TableInfoErr:
Select Case Err
Case 3265& 'Table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
Debug.Print "TableInfo() Error " & Err & ": " & Error
End Select
Resume TableInfoExit
End Function
'===================================


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
Steve

That is what I was looking for, but every time I run the code:

Select Case VarType(List67.ItemData(varnumber))
Evaluates as a string. The entire code is below. Is there some other way I
should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer

sfld = Me.GRPSELECT

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.openreport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
Debug.Print VarType(List67.ItemData(varnumber))
Select Case VarType(List67.ItemData(varnumber))
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal
separ = ""
Case Else
Debug.Print VarType(List67.ItemData(varnumber))
End Select
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

--
Ficticiously Yours, Biggles


Steve Sanford said:
Check out the VarType() function.

This is air code, but it might help...

' I added an 's' here
ssepar = "'"
dsepar = "#"
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected

Select Case VarType(List67.ItemData(varnumber))
'for dates
Case vbDate ' constant = 7
sfilt = sfilt & dsepar & List67.ItemData(varnumber) & dsepar & ","
'for strings
Case vbString 'constant = 8
sfilt = sfilt & ssepar & List67.ItemData(varnumber) & ssepar & ","
'for numbers
Case vbInteger - vbCurrency, vbDecimal 'constants 2-6 & 14
sfilt = sfilt & List67.ItemData(varnumber) & ","
End Select
Next


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
But how do I identify the field type for sfld or me.grpselect?
--
Ficticiously Yours, Biggles


:

Put the logic in a function and pass the separator as a parameter.

Dorian

:

I populate a combo box with a field list, which, when selected will populate
a list box using this code:
Dim SSQL As String

SSQL = "SELECT ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] FROM
ARCHIVED_ISSUES GROUP BY ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] "

Me.List67.RowSource = SSQL

That part works fine, but I get errors when I try to build a list with the
different selections from the multi-select list box, since the values could
be strings, numbers, dates. So, how do I make this:

separ = "'"

createlist:
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

flexible enough to be able to change the separ constant to be "'", "", or "#".

Sean
 
G

Guest

Everytime I run the function, regardless of what field is selected, I get
FLDTYPE = 10, a vbERROR. Am I missing something?

Private Sub Command72_Click()
On Error GoTo Err_Command72_Click

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As String

Dim stlinkcriteria As String
Dim separ As String
Dim FldType As Long '*****

sfld = Me.GRPSELECT
'FldType = Null

stDocName = "rptALLARCHIVED_BASE"

'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.openreport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

FldType = TableInfo("ARCHIVED_ISSUES", sfld)
'Debug.Print FldType
Select Case FldType
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal, vbSingle, vbDouble
separ = ""
Case Else
Debug.Print FldType
End Select

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next
Debug.Print sfilt

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
End If
Debug.Print sfilt

DoCmd.openreport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

End Sub

'========================================

'the following code (modified) was shamelessly stolen from
'Allen Browne at http://allenbrowne.com/func-06.html

Function TableInfo(strTableName As String, strFieldName As String) As Long
On Error GoTo TableInfoErr
' Purpose: Determine the field type in a table
' Arguments: Name of a table in the current database, Name of a field in
the Table
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Debug.Print strFieldName
Set db = CurrentDb()
Set tdf = db.TableDefs(strTableName)

For Each fld In tdf.Fields
If fld.Name = strFieldName Then
TableInfo = CLng(fld.Type) 'fld.Type is Integer, but constants are
Long.
Debug.Print TableInfo
End If
Next

TableInfoExit:
Set db = Nothing
Exit Function

TableInfoErr:
Select Case Err
Case 3265& 'Table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
Debug.Print "TableInfo() Error " & Err & ": " & Error
End Select
Resume TableInfoExit
End Function
'===================================

--
Ficticiously Yours, Biggles


Steve Sanford said:
My bad, Biggles. I didn't think it through.

OK, so you have a combo box where you pick a field, which populates a list
box with the data from one field. The data can/might change each time you run
the report. One time you might be searching for records for certain dates,
other times it might be for certain text in the field.

So the trick is determining the field type using the field name. This is
done using a function "TableInfo()" passing two arguments: the table name and
the field name.
Below is the code:

' Select Case VarType(List67.ItemData(varnumber))
' Evaluates as a string. The entire code is below. Is there some
other way I
' should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer
Dim FldType As Long '*****

sfld = Me.GRPSELECT
FldType = Null

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.OpenReport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'determine the delimiter needed
FldType = TableInfo("ARCHIVED_ISSUES", sfld)
Select Case FldType
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal, vbSingle, vbDouble
separ = ""
Case Else
Debug.Print FldType
End Select

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
'Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
' lngLen = Len(sfilt) - 1
' If lngLen > 0 Then
If Len(sfilt) > 0 Then
sfilt = "[" & sfld & "] in (" & Left(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.OpenReport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

' I think this is where you would have an
'END SUB

'========================================

'the following code (modified) was shamelessly stolen from
'Allen Browne at http://allenbrowne.com/func-06.html

Function TableInfo(strTableName As String, strFieldName As String) As Long
On Error GoTo TableInfoErr
' Purpose: Determine the field type in a table
' Arguments: Name of a table in the current database, Name of a field in
the table
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Set db = CurrentDb()
Set tdf = db.TableDefs(strTableName)

For Each fld In tdf.Fields
If fld.Name = strFieldName Then
TableInfo = CLng(fld.Type) 'fld.Type is Integer, but constants are
Long.
End If
Next

TableInfoExit:
Set db = Nothing
Exit Function

TableInfoErr:
Select Case Err
Case 3265& 'Table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
Debug.Print "TableInfo() Error " & Err & ": " & Error
End Select
Resume TableInfoExit
End Function
'===================================


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
Steve

That is what I was looking for, but every time I run the code:

Select Case VarType(List67.ItemData(varnumber))
Evaluates as a string. The entire code is below. Is there some other way I
should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer

sfld = Me.GRPSELECT

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.openreport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
Debug.Print VarType(List67.ItemData(varnumber))
Select Case VarType(List67.ItemData(varnumber))
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal
separ = ""
Case Else
Debug.Print VarType(List67.ItemData(varnumber))
End Select
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

--
Ficticiously Yours, Biggles


Steve Sanford said:
Check out the VarType() function.

This is air code, but it might help...

' I added an 's' here
ssepar = "'"
dsepar = "#"
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected

Select Case VarType(List67.ItemData(varnumber))
'for dates
Case vbDate ' constant = 7
sfilt = sfilt & dsepar & List67.ItemData(varnumber) & dsepar & ","
'for strings
Case vbString 'constant = 8
sfilt = sfilt & ssepar & List67.ItemData(varnumber) & ssepar & ","
'for numbers
Case vbInteger - vbCurrency, vbDecimal 'constants 2-6 & 14
sfilt = sfilt & List67.ItemData(varnumber) & ","
End Select
Next


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


:

But how do I identify the field type for sfld or me.grpselect?
--
Ficticiously Yours, Biggles


:

Put the logic in a function and pass the separator as a parameter.

Dorian

:

I populate a combo box with a field list, which, when selected will populate
a list box using this code:
Dim SSQL As String

SSQL = "SELECT ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] FROM
ARCHIVED_ISSUES GROUP BY ARCHIVED_ISSUES.[" & Me.GRPSELECT & "] "

Me.List67.RowSource = SSQL

That part works fine, but I get errors when I try to build a list with the
different selections from the multi-select list box, since the values could
be strings, numbers, dates. So, how do I make this:

separ = "'"

createlist:
'if users select records to filter by in the list box
sfilt = ""
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

flexible enough to be able to change the separ constant to be "'", "", or "#".

Sean
 
G

Guest

My apologies. The TableInfo() function was returning a DAO field type
constant and I was comparing it to VBA VarType Constants.

Here are both modified Subs:

'========================
Private Sub Command72_Click()
On Error GoTo Err_Command72_Click

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As String

Dim stlinkcriteria As String
Dim separ As String
Dim FldType As Long '*****

sfld = Me.GRPSELECT
FldType = Null

stDocName = "rptALLARCHIVED_BASE"

'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.OpenReport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

FldType = TableInfo("ARCHIVED_ISSUES", sfld)
'Debug.Print FldType
Select Case FldType
Case dbDate 'date fields
separ = "#"
Case dbText 'character fields
separ = "'"
Case dbInteger, dbLong, dbSingle, dbDouble, dbCurrency, dbDecimal
separ = ""
Case Else
Debug.Print FldType
End Select

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next
Debug.Print sfilt

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
End If
Debug.Print sfilt

DoCmd.OpenReport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

End Sub

'----------------------------------

'the following code (modified) was shamelessly stolen from
'Allen Browne at http://allenbrowne.com/func-06.html

Function TableInfo(strTableName As String, strFieldName As String) As Long
On Error GoTo TableInfoErr
' Purpose: Determine the field type in a table
' Argument: Name of a table in the current database, Name of a field in
the table
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Set db = CurrentDb()
Set tdf = db.TableDefs(strTableName)

For Each fld In tdf.Fields
If fld.Name = strFieldName Then
TableInfo = CLng(fld.Type) 'fld.Type is Integer, but constants are
Long.
End If
Next

TableInfoExit:
Set db = Nothing
Set tdf = Nothing
Set fld = Nothing
Exit Function

TableInfoErr:
Select Case Err
Case 3265& 'Table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
Debug.Print "TableInfo() Error " & Err & ": " & Error
End Select
Resume TableInfoExit
End Function
'===========================


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
Everytime I run the function, regardless of what field is selected, I get
FLDTYPE = 10, a vbERROR. Am I missing something?

Private Sub Command72_Click()
On Error GoTo Err_Command72_Click

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As String

Dim stlinkcriteria As String
Dim separ As String
Dim FldType As Long '*****

sfld = Me.GRPSELECT
'FldType = Null

stDocName = "rptALLARCHIVED_BASE"

'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.openreport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

FldType = TableInfo("ARCHIVED_ISSUES", sfld)
'Debug.Print FldType
Select Case FldType
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal, vbSingle, vbDouble
separ = ""
Case Else
Debug.Print FldType
End Select

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Next
Debug.Print sfilt

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
End If
Debug.Print sfilt

DoCmd.openreport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

End Sub

'========================================

'the following code (modified) was shamelessly stolen from
'Allen Browne at http://allenbrowne.com/func-06.html

Function TableInfo(strTableName As String, strFieldName As String) As Long
On Error GoTo TableInfoErr
' Purpose: Determine the field type in a table
' Arguments: Name of a table in the current database, Name of a field in
the Table
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Debug.Print strFieldName
Set db = CurrentDb()
Set tdf = db.TableDefs(strTableName)

For Each fld In tdf.Fields
If fld.Name = strFieldName Then
TableInfo = CLng(fld.Type) 'fld.Type is Integer, but constants are
Long.
Debug.Print TableInfo
End If
Next

TableInfoExit:
Set db = Nothing
Exit Function

TableInfoErr:
Select Case Err
Case 3265& 'Table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
Debug.Print "TableInfo() Error " & Err & ": " & Error
End Select
Resume TableInfoExit
End Function
'===================================

--
Ficticiously Yours, Biggles


Steve Sanford said:
My bad, Biggles. I didn't think it through.

OK, so you have a combo box where you pick a field, which populates a list
box with the data from one field. The data can/might change each time you run
the report. One time you might be searching for records for certain dates,
other times it might be for certain text in the field.

So the trick is determining the field type using the field name. This is
done using a function "TableInfo()" passing two arguments: the table name and
the field name.
Below is the code:

' Select Case VarType(List67.ItemData(varnumber))
' Evaluates as a string. The entire code is below. Is there some
other way I
' should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer
Dim FldType As Long '*****

sfld = Me.GRPSELECT
FldType = Null

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.OpenReport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'determine the delimiter needed
FldType = TableInfo("ARCHIVED_ISSUES", sfld)
Select Case FldType
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal, vbSingle, vbDouble
separ = ""
Case Else
Debug.Print FldType
End Select

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
'Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
' lngLen = Len(sfilt) - 1
' If lngLen > 0 Then
If Len(sfilt) > 0 Then
sfilt = "[" & sfld & "] in (" & Left(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.OpenReport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
Resume Exit_Command72_Click

' I think this is where you would have an
'END SUB

'========================================

'the following code (modified) was shamelessly stolen from
'Allen Browne at http://allenbrowne.com/func-06.html

Function TableInfo(strTableName As String, strFieldName As String) As Long
On Error GoTo TableInfoErr
' Purpose: Determine the field type in a table
' Arguments: Name of a table in the current database, Name of a field in
the table
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field

Set db = CurrentDb()
Set tdf = db.TableDefs(strTableName)

For Each fld In tdf.Fields
If fld.Name = strFieldName Then
TableInfo = CLng(fld.Type) 'fld.Type is Integer, but constants are
Long.
End If
Next

TableInfoExit:
Set db = Nothing
Exit Function

TableInfoErr:
Select Case Err
Case 3265& 'Table name invalid
MsgBox strTableName & " table doesn't exist"
Case Else
Debug.Print "TableInfo() Error " & Err & ": " & Error
End Select
Resume TableInfoExit
End Function
'===================================


HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Biggles said:
Steve

That is what I was looking for, but every time I run the code:

Select Case VarType(List67.ItemData(varnumber))
Evaluates as a string. The entire code is below. Is there some other way I
should format the list box?

Dim stDocName As String
Dim varnumber As Variant
Dim sfilt As String
Dim lngLen As Long
Dim sfld As Variant

Dim stlinkcriteria As String
Dim separ As String
Dim sepctr As Integer

sfld = Me.GRPSELECT

stDocName = "rptALLARCHIVED_BASE"
Debug.Print Me.Option69
'If users choose the all records option
If Me.Option69 = -1 Then
DoCmd.openreport stDocName, acPreview
Exit Sub
End If
On Error GoTo Err_Command72_Click

'if users select records to filter by in the list box
For Each varnumber In List67.ItemsSelected
Debug.Print VarType(List67.ItemData(varnumber))
Select Case VarType(List67.ItemData(varnumber))
Case vbDate 'date fields
separ = "#"
Case vbString 'character fields
separ = "'"
Case vbInteger, vbCurrency, vbDecimal
separ = ""
Case Else
Debug.Print VarType(List67.ItemData(varnumber))
End Select
sfilt = sfilt & separ & List67.ItemData(varnumber) & separ & ","
Debug.Print sfilt
Next

'remove trailing comma, add field name, in operator and brackets
lngLen = Len(sfilt) - 1
If lngLen > 0 Then
sfilt = "[" & sfld & "] in (" & Left$(sfilt, lngLen) & ")"
Debug.Print sfilt
End If

DoCmd.openreport stDocName, acPreview, , sfilt

Exit_Command72_Click:
Exit Sub

Err_Command72_Click:
Debug.Print Err.Number; Err.Description
 
G

Guest

Steve

That worked great, thanks for all your help. And I stole your tag line for
my board for a few weeks, I like it.

Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top