SQL to VBA Conversion (1 Viewer)

Oreynolds

Member
Local time
Today, 14:29
Joined
Apr 11, 2020
Messages
157
Hi,

I have the following working SQL which I need to convert to VBA which I have done. When I run my VBA I get a syntax error which through a process of elimination relates to the embedded IIF statement. Can anyone see where I have gone wrong with this?

SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate
FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef
WHERE (((Orders.ProjectManager)=IIf([Forms]![JobCostReportFilters]![Combo0]=0,[Orders].[ProjectManager],[Forms]![JobCostReportFilters]![Combo0])) AND ((Orders.ClosedDate) Between [Forms]![JobCostReportFilters]![txtStartDate] And [Forms]![JobCostReportFilters]![txtEndDate]))
ORDER BY Min(tblVisits.VisitDate);

Code:
Set rs = CurrentDb.OpenRecordset(" SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
" FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
" WHERE ((Orders.ProjectManager = IIf(" & Me.Combo0 & " = 0,Orders.ProjectManager, " & Me.Combo0 & ")) " & _
" AND (Orders.ClosedDate BETWEEN DateValue('" & Me.txtStartDate & "') AND DateValue('" & Me.txtEndDate & "')) " & _
" ORDER BY Min(tblVisits.VisitDate);")
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:29
Joined
Oct 29, 2018
Messages
21,454
Hi. What was the error message? Was it 3061 Too few parameters? If so, you could try using this generic function.

 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
14,235
I am going to go to my grave saying this :(

Put the sql into a string variable and debug.print it.
If you still cannot see your error post the output here.

You can also test the output in the qbe sql window and that should show you the error?
 

Oreynolds

Member
Local time
Today, 14:29
Joined
Apr 11, 2020
Messages
157
Hi. What was the error message? Was it 3061 Too few parameters? If so, you could try using this generic function.

1644776203656.png
 

Oreynolds

Member
Local time
Today, 14:29
Joined
Apr 11, 2020
Messages
157
Ah, syntax error. This doesn't look like it's for a passthrough query. If so, you should use the # symbol, rather than single quotes to delimit the date values.

Hi, no the date part of the string works fine, it only errors when I add the IIF statement back into the WHERE clause
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:29
Joined
Oct 29, 2018
Messages
21,454
Hi, no the date part of the string works fine, it only errors when I add the IIF statement back into the WHERE clause
Okay, try modifying this part to:
Code:
" WHERE ((Orders.ProjectManager = " & IIf(Me.Combo0 = 0, Orders.ProjectManager, Me.Combo0) & ") " & _
(untested)
Hope that helps...
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
14,235
Hi, no the date part of the string works fine, it only errors when I add the IIF statement back into the WHERE clause
Are you sure?, as whilst the qbe accepts dd/mm/yyyy it coverts it to mm/dd/yyy in the sql
It even surround the date with # when you go back to qbe window?

1644779033432.png


Code:
SELECT Transactions.*, Transactions.TransactionDate
FROM Transactions
WHERE (((Transactions.TransactionDate)=#1/8/2016#));

1644779094267.png


1644779218602.png
 

isladogs

MVP / VIP
Local time
Today, 14:29
Joined
Jan 14, 2017
Messages
18,209

GPGeorge

Grover Park George
Local time
Today, 06:29
Joined
Nov 25, 2004
Messages
1,829
Hi,

I have the following working SQL which I need to convert to VBA which I have done. When I run my VBA I get a syntax error which through a process of elimination relates to the embedded IIF statement. Can anyone see where I have gone wrong with this?

SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate
FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef
WHERE (((Orders.ProjectManager)=IIf([Forms]![JobCostReportFilters]![Combo0]=0,[Orders].[ProjectManager],[Forms]![JobCostReportFilters]![Combo0])) AND ((Orders.ClosedDate) Between [Forms]![JobCostReportFilters]![txtStartDate] And [Forms]![JobCostReportFilters]![txtEndDate]))
ORDER BY Min(tblVisits.VisitDate);

Code:
Set rs = CurrentDb.OpenRecordset(" SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
" FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
" WHERE ((Orders.ProjectManager = IIf(" & Me.Combo0 & " = 0,Orders.ProjectManager, " & Me.Combo0 & ")) " & _
" AND (Orders.ClosedDate BETWEEN DateValue('" & Me.txtStartDate & "') AND DateValue('" & Me.txtEndDate & "')) " & _
" ORDER BY Min(tblVisits.VisitDate);")
Check the SQL Converter on the AUG website. I have only used it a couple of times myself, but it does offer a SQL --> VBA conversion. See if that does the job. If so, please let us know. Heck, one way or the other I'd like to know how good it is.
 

GPGeorge

Grover Park George
Local time
Today, 06:29
Joined
Nov 25, 2004
Messages
1,829
FWIW: THis is what the AUG SQL Converter produced from the SQL in your initial post.

strSQL = "SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
"FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
"WHERE (((Orders.ProjectManager)=IIf([Forms]![JobCostReportFilters]![Combo0]=0,[Orders].[ProjectManager],[Forms]![JobCostReportFilters]![Combo0])) AND ((Orders.ClosedDate) Between [Forms]![JobCostReportFilters]![txtStartDate] And [Forms]![JobCostReportFilters]![txtEndDate])) " & _
"ORDER BY Min(tblVisits.VisitDate); " & _
""
 

Oreynolds

Member
Local time
Today, 14:29
Joined
Apr 11, 2020
Messages
157
Are you sure?, as whilst the qbe accepts dd/mm/yyyy it coverts it to mm/dd/yyy in the sql
It even surround the date with # when you go back to qbe window?

View attachment 98225

Code:
SELECT Transactions.*, Transactions.TransactionDate
FROM Transactions
WHERE (((Transactions.TransactionDate)=#1/8/2016#));

View attachment 98226

View attachment 98227

Yep, the dates definitely work as written, I know what you mean re # in query criteria
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:29
Joined
Oct 29, 2018
Messages
21,454
Yep, the dates definitely work as written, I know what you mean re # in query criteria
Hi. Have you had a chance to try my suggestion yet? Just curious...
 

Oreynolds

Member
Local time
Today, 14:29
Joined
Apr 11, 2020
Messages
157
Hi. Have you had a chance to try my suggestion yet? Just curious...

Hi yes I have, unfortunately that didn't work either. I have found a solution of sorts which is to create two SQL strings as below and perform the IF statement out of the SQL string. This works but obviously slightly more code.....

This is the working code:

Code:
'Calculate and populate the earliest visit date for labour used calcs
If Me.Combo0 = 0 Then

strSQL = " SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
" FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
" WHERE (Orders.ClosedDate BETWEEN DateValue('" & Me.txtStartDate & "') AND DateValue('" & Me.txtEndDate & "')) " & _
" ORDER BY Min(tblVisits.VisitDate);"

Else

strSQL = " SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
" FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
" WHERE (Orders.ProjectManager = " & Me.Combo0 & ") AND (Orders.ClosedDate BETWEEN DateValue('" & Me.txtStartDate & "') AND DateValue('" & Me.txtEndDate & "')) " & _
" ORDER BY Min(tblVisits.VisitDate);"

End If
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:29
Joined
Sep 21, 2011
Messages
14,235
Why not just add the projectmanager criteria if combo not 0?
Switch the order of the criteria?
 

Oreynolds

Member
Local time
Today, 14:29
Joined
Apr 11, 2020
Messages
157
Why not just add the projectmanager criteria if combo not 0?
Switch the order of the criteria?
Yer that is what I was trying to do, I presume you mean just have one SQL string and then add the additional criteria? But I couldn’t get my head round getting that to work….?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:29
Joined
Oct 29, 2018
Messages
21,454
Hi yes I have, unfortunately that didn't work either. I have found a solution of sorts which is to create two SQL strings as below and perform the IF statement out of the SQL string. This works but obviously slightly more code.....

This is the working code:

Code:
'Calculate and populate the earliest visit date for labour used calcs
If Me.Combo0 = 0 Then

strSQL = " SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
" FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
" WHERE (Orders.ClosedDate BETWEEN DateValue('" & Me.txtStartDate & "') AND DateValue('" & Me.txtEndDate & "')) " & _
" ORDER BY Min(tblVisits.VisitDate);"

Else

strSQL = " SELECT Min(tblVisits.VisitDate) AS MinOfVisitDate " & _
" FROM Orders INNER JOIN (tblVisits INNER JOIN tblJobs ON tblVisits.JobID = tblJobs.JobID) ON Orders.OrderNumber = tblJobs.InternalRef " & _
" WHERE (Orders.ProjectManager = " & Me.Combo0 & ") AND (Orders.ClosedDate BETWEEN DateValue('" & Me.txtStartDate & "') AND DateValue('" & Me.txtEndDate & "')) " & _
" ORDER BY Min(tblVisits.VisitDate);"

End If
Hi. Just curious, you were originally having a syntax error. Are you saying when you tried my suggestion, the syntax error didn't go away?
 

Users who are viewing this thread

Top Bottom