Hi again Dave
Before I get into detail I'm going to give an overview of what is/should be
happening
A button is clicked which runs cmdViewReport_Click
cmdViewReport_Click calls BuildSQLString(strSQL) using this line of code...
If Not BuildSQLString(strSQL) Then
Note that strSQL is blank at this point.
BuildSQLString then builds strSQL
When BuildSQLString is finished it is supposed to return a value of True or
False so as to indicate whether it successfully built the string or not. Note
- very important to understand that it doesn't return the string - it returns
True or False.
If it returns False then a message is displayed - and, I assume, the code
should stop and the report shouldn't be run.
If it returns True then the report can run.
So that's the plan.
However, your BuildSQLString funtion always returns True so I don't see why
the function is needed at all. I don't know whether you have changed it a lot
from what was in the book you said you got it from. Personally, I wouldn't
use the function and I would build the strSQL inside the sub
cmdViewReport_Click.
Having said that, if I just comment on the syntax of the code in
BuildSQLString it all now looks ok. The only thing I noticed is the variable
chkDOT is declared anywhere. What is this?
You said that BuildSQLString showed nothing in the immediate window. I tried
this also and got the same so typed the following (in immediate)...
msgbox buildsqlstring("")
and this gave a message of True. So you can see it's working.
Now let's move on to the sub cmdViewReport_Click.
I have pasted this whole below, and made comments in line...
Private Sub cmdViewReport_Click()
On Error GoTo Err_cmdViewReport_Click
Dim strSQL As String
Dim strDocName As String
If Not BuildSQLString(strSQL) Then
MsgBox "There was a problem building query for the report"
End If
***************
As said above, it never returns False so this message is never displayed.
BUT!!! If it did then I assume you would want to stop the code here but the
code doesn't stop. So, even if BuildSQLString returned False and the message
was displayed it would still carry on and try to run the report.
The code should probably be...
If Not BuildSQLString(strSQL) Then
MsgBox "There was a problem building query for the report"
Exit Sub
End If
***************
strDocName = "rptNumberedFleet"
DoCmd.OpenReport strDocName, acViewPreview
***************
So this is where the report is opened and then you get the prompts.
I will talk about the prompts lower down.
The big point to be made here is that all effort up to this point has been
to build the variable strSQL which I believe was so it could be used in the
final step when the report was run.
However, strSQL is NOT used by this report rptNumberedFleet.
If you go into the design of the report itself you will see what its source
query is.
Go into that query and you will be able to see the SQL it is using - and it
is this SQL that is running - NOT the SQL built into strSQL - but see next
comments...
***************
'MsgBox strSQL
CurrentDb.QueryDefs("qryParameters").SQL = strSQL
***************
This looks promising! I am guessing that the report report rptNumberedFleet
uses the query qryParameters in some way so this is how we can get the report
to use the strSQL we have built.
However, shouldn't this command come before we open the report?
(It probably seems to work anyway because the SQL in the query has already
been set from previous runs of the code).
***************
Exit_cmdViewReport_Click:
Exit Sub
Hope I've made sense so far. Now the prompts.
These prompts are all to do with the query that the report is running.
It means that Access doesn't know what they are.
If we look at strSQL we can see where they appear.
(1) tblDOTMLPF.DOTMLP_Choices
Check there is a table tblDOTMLPF (check spelling carefully!)
And, does this table have a column called DOTMLP_Choices
To me, it looks like a letter F is missing from just before the _Choices.
(2) DOTMLPF_Choices
This adds a bit more confusion. It helps to confirm that item (1) has a
missing letter F (because this item does have an F). But, if it was right it
wouldn't be prompted for so I can only get you to check the column names of
all the tables in the query. I notice that this reference doesn't use a table
name so if this column is spelt correctly but appears in more than 1 table it
will give an error. So, check it is spelt correctly and also check which
table you want to use it from and add the table name to your code.
(3) cboDOT
As you have used the prefix cbo I am assuming this is the name of a combo box.
When a query uses a combo box value it has to use the following syntax...
Forms![MyForm]![cboDOT]
You will replace MyForm with the name of the form the combo box is on.
The line of code affected is...
If chkDOT Then
strWhere = strWhere & " AND DOTMLPF_Choices = " & "[cboDOT]"
End If
If cboDOT is a number, change it to...
strWhere = strWhere & " AND DOTMLPF_Choices = " & Forms![MyForm]![cboDOT]
If cboDOT is text, change it to...
strWhere = strWhere & " AND DOTMLPF_Choices = """ &
"Forms![MyForm]![cboDOT]" & """"
(Every quote character is a double quote. Where they are repeated there are
3 before the first & and 4 at the end)
As regards your 8 combo boxes my approach would be to get it working with
one of them first. Then when you know you have the right syntax you can add
the others into the code.
A question that might change your approach...
Is it only the Where clause that is dynamic?
If so, you don't need to build the SQL every time. You can go into the
query design and put the combo box references directly in there.
hth
Andy Hull
Andy
First I appreciate your help. I am doing this as a collateral duty for
work with the Navy as I have time in the evenings because I am one of
the main users of the Dbase for research purposes and can not get the
data in a usable format.
I have frmParameters with several check boxes (like chkDOT) that when
chosen enables a ComboBox (cboDOT) that is used to build string to use
to create a report. There are multiple combo boxes that can set
criteria for the query and I will use more than one at a time. They
do not all use the same tables or fields so I do need to keep the
"Select" and "From" in the function. THe report will be standard for
all queries. I also will have to be able to email a form to users. I
need to use a form because there are hyperlinks tha I require to be
actiove whcih you can not do in a report unless you export it to some
other format and I might end up doing that but first I need to get my
query figured out.
I have created and tested MakeQueryDef and ChangeQueryDef Module as
follows:
Option Compare Database
Function MakeQueryDef(strSQL As String) As Boolean
Dim qdf As QueryDef
If strSQL = "" Then Exit Function
Set qdf = CurrentDb.CreateQueryDef("qryParameters")
qdf.SQL = strSQL
qdf.Close
RefreshDatabaseWindow
MakeQueryDef = True
End Function
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++
Function ChangeQueryDef(strQuery As String, strSQL As String) As
Boolean
If strQuery = "" Or strSQL = "" Then Exit Function
Dim qdf As QueryDef
Set qdf = CurrentDb.QueryDefs(strQuery)
qdf.SQL = strSQL
qdf.Close
RefreshDatabaseWindow
ChangeQueryDef = True
End Function
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++=
I thought these functions would set the record source for my
rptParameters as qryParameters
I have not modified the routine much except for the "Select" and
"from" clauses as they were consistent in the example I am following.
tblBasicData and tblComments are common throughout the queries but the
third table will change based on which chkBox is chosen
I thought that if the BuildSQLString function could not put together
an understandable query (would this be False return) it would return
"There was a problem...." from the If Not BuildSQLString(strSQL).
I fixed all the parameter request problems that came up but the report
does not populate
when I enter ?BuildSQLString("") in immediate window I am getting
"mismatch error" but I can not see a type mismatch
Here is the code as it currently sits:
Private Sub cmdViewReport_Click()
On Error GoTo Err_cmdViewReport_Click
Dim strSQL As String
Dim strDocName As String
If Not BuildSQLString(strSQL) Then
MsgBox "There was a problem building query for the report" never
seen this and when I comment in MsgBox strSQL a message box appears
witht he correct SQL string
Exit Sub
End If
strDocName = "rptNumberedFleet"
CurrentDb.QueryDefs("qryParameters").SQL = strSQL
DoCmd.OpenReport strDocName, acViewPreview
' MsgBox strSQL
Exit_cmdViewReport_Click:
Exit Sub
Err_cmdViewReport_Click:
MsgBox Err.Description
Resume Exit_cmdViewReport_Click
End Sub
Public Function BuildSQLString(strSQL As String) As Boolean
Dim strSelect As String
Dim strFrom As String
Dim strWhere As String
strSelect = "tblBasicData.Title, tblBasicData.HyperlinkToLesson,
tblComments.solution, tblDOTMLPF.DOTMLPF_Choices"
strFrom = "tblDOTMLPF INNER JOIN (tblBasicData INNER JOIN tblComments
ON tblBasicData.[Lesson IDPK] = tblComments.Lesson_IDFK) ON tblDOTMLPF.
[DOTMLPF ID PK] = tblComments.DOTLMPF_ChoiceFK"
If chkDOT Then
strWhere = strWhere & " AND DOTMLPF_Choices =""" & "Forms!
[frmParameters]![cboDOT]" & """"
End If
'IF chkPersonnel Then
'strWhere =strWhere & "...........
' End If
strSQL = "Select " & strSelect
strSQL = strSQL & " From " & strFrom
If strWhere <> "" Then strSQL = strSQL & " WHERE " & Mid$(strWhere, 6)
BuildSQLString = True
End Function
If I can get one (or should I say "we" becasue I really do appreciate
and need your help) I hope I can get the others until the last one
which is bsed on a query in a query. I am learning a alot
Thanks
Dave in Chesapeake VA