Here is what I have.
I've got a table with 43 fields that I want to query for a datasheet(for my example code I'll only use 5). I want the user to be able to enter critereon to filter each field if they want to. So on one form I have listed checkboxes for all the fields. If they want the field listed in the datasheet they click the checkbox (-1). If they don't, then they leave it "0". I'm able to use those checkbox values on the form.load for the datasheet by using Field.ColumnHidden if the checkbox value = "0". All this works fine.
I ran into problems where I tried to add critereon to the query. Here's the code, I appologize in advance if my code or explanation is lacking. I often come up with answers to my problems in a very non-traditional way...so sometimes because of the way I code things, I write way too much code than necessary.
I create the RecordSource for the datasheet(form) with this:
Sub createdatasheetrecordsource()
Dim drs As Variant 'variable designed to concatonate strings to form the RecordSource
drs = "SELECT MEMBERtbl.SSN, MEMBERtbl.RANK, MEMBERtbl.FIRSTNAME, MEMBERtbl.MIDDLENAME, MEMBERtbl.LASTNAME" _
& "FROM MEMBERtbl " _
& "WHERE (((MEMBERtbl.SSN) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![SSNz])" 'the code thus far works fine by itself. It will create the datasheet and my ColumnHidden code removes the columns I didn't want in the datasheet
'The problem is here at the point I try to add Critereon to the Where statement... I figure I've misplaced a "space" somewhere, or thru in an extra ( or ". Can't seem to figure out what I'm doing wrong.
If RANK.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.RANK) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![RANKz])"
End If
If FIRSTNAME.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.FIRSTNAME) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![FIRSTNAMEz] & " * ")"
End If
If MIDDLENAME.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.MIDDLENAME) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![MIDDLENAMEz] & " * ")"
End If
If LASTNAME.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.LASTNAME) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![LASTNAMEz] & " * ")"
End If
drs = drs & ");" 'ends the WHERE statement
DOT.SetFocus
DoCmd.OpenForm "DATASHEETfrm"
Forms!DATASHEETfrm!DATASHEETRECORDSOURCE = drs
Forms!DATASHEETfrm!DATASHEETSUBfrm.SourceObject = "DATASHEETSUBfrm"
End Sub
If I've totally confused you, again I'm sorry... If you think you can fix it if you could see it... I can try to make a mock .mdb and upload it to a website for download. Just let me know. Thanks again in advance,
Snayjay
I've got a table with 43 fields that I want to query for a datasheet(for my example code I'll only use 5). I want the user to be able to enter critereon to filter each field if they want to. So on one form I have listed checkboxes for all the fields. If they want the field listed in the datasheet they click the checkbox (-1). If they don't, then they leave it "0". I'm able to use those checkbox values on the form.load for the datasheet by using Field.ColumnHidden if the checkbox value = "0". All this works fine.
I ran into problems where I tried to add critereon to the query. Here's the code, I appologize in advance if my code or explanation is lacking. I often come up with answers to my problems in a very non-traditional way...so sometimes because of the way I code things, I write way too much code than necessary.
I create the RecordSource for the datasheet(form) with this:
Sub createdatasheetrecordsource()
Dim drs As Variant 'variable designed to concatonate strings to form the RecordSource
drs = "SELECT MEMBERtbl.SSN, MEMBERtbl.RANK, MEMBERtbl.FIRSTNAME, MEMBERtbl.MIDDLENAME, MEMBERtbl.LASTNAME" _
& "FROM MEMBERtbl " _
& "WHERE (((MEMBERtbl.SSN) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![SSNz])" 'the code thus far works fine by itself. It will create the datasheet and my ColumnHidden code removes the columns I didn't want in the datasheet
'The problem is here at the point I try to add Critereon to the Where statement... I figure I've misplaced a "space" somewhere, or thru in an extra ( or ". Can't seem to figure out what I'm doing wrong.
If RANK.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.RANK) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![RANKz])"
End If
If FIRSTNAME.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.FIRSTNAME) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![FIRSTNAMEz] & " * ")"
End If
If MIDDLENAME.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.MIDDLENAME) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![MIDDLENAMEz] & " * ")"
End If
If LASTNAME.Value = -1 Then
drs = drs & " AND ((MEMBERtbl.LASTNAME) Like [Forms]![CREATEQUERYfrm]![CREATEQUERYSUBfrm]![LASTNAMEz] & " * ")"
End If
drs = drs & ");" 'ends the WHERE statement
DOT.SetFocus
DoCmd.OpenForm "DATASHEETfrm"
Forms!DATASHEETfrm!DATASHEETRECORDSOURCE = drs
Forms!DATASHEETfrm!DATASHEETSUBfrm.SourceObject = "DATASHEETSUBfrm"
End Sub
If I've totally confused you, again I'm sorry... If you think you can fix it if you could see it... I can try to make a mock .mdb and upload it to a website for download. Just let me know. Thanks again in advance,
Snayjay