String SQL statement not returning value

T

Todd H

Dim dbs As Database
Dim strCurPer As String
Dim intCurPer As Integer
Set dbs = CurrentDb
strCurPer = "SELECT BillingPeriodNumber FROM tblBillingPeriod" &
"WHERE [tblBillingPeriod.BillingPeriodEndDate] Between" & "#" &
Format(DateAdd("m", -4, Date), "mm/dd/yyyy") & "# And #" _
& Format(DateAdd("m", -3, Date), "mm/dd/yyyy") & "#"
'MsgBox to Troubleshoot
MsgBox ("strCurPer is" & [strCurPer])
intCurPer = Val(strCurPer)

I am trying to have this SQL statement return a BillingPeriodNumber
and then turn that String value into an Integer. I added the text box
to see the value of strCurPer and now it returns the text of the code
with the dates calculated. What am I doing wrong?
 
M

Marshall Barton

Todd said:
Dim dbs As Database
Dim strCurPer As String
Dim intCurPer As Integer
Set dbs = CurrentDb
strCurPer = "SELECT BillingPeriodNumber FROM tblBillingPeriod" &
"WHERE [tblBillingPeriod.BillingPeriodEndDate] Between" & "#" &
Format(DateAdd("m", -4, Date), "mm/dd/yyyy") & "# And #" _
& Format(DateAdd("m", -3, Date), "mm/dd/yyyy") & "#"
'MsgBox to Troubleshoot
MsgBox ("strCurPer is" & [strCurPer])
intCurPer = Val(strCurPer)

I am trying to have this SQL statement return a BillingPeriodNumber
and then turn that String value into an Integer. I added the text box
to see the value of strCurPer and now it returns the text of the code
with the dates calculated. What am I doing wrong?


That string expression needs a space before and after all
the keywords.

You would need to open a recordset on the query to retrieve
the value.

However, all you need is to use the DLookup function to get
the value:

intCurPer = DLookup("BillingPeriodNumber" _
, "tblBillingPeriod" _
, "BillingPeriodEndDate Between " _
& Format(DateAdd("m", -4, Date), "\#m\/d\/yyyy\#") _
& " And " _
& Format(DateAdd("m", -3, Date), "\#m\/d\/yyyy\#")
 
M

Marshall Barton

Todd H wrote:

[snip unneeded code]
I am trying to have this SQL statement return a BillingPeriodNumber
and then turn that String value into an Integer. I added the text box
to see the value of strCurPer and now it returns the text of the code
with the dates calculated. What am I doing wrong?


After using the DLookup I suggested in your other thread:

WIth Me.Recordset
If .RecordCount > 0 Then
.FindFirst "BillingPeriodNumber = " & intCurPer
End If
End With
 
T

Todd H

Todd H wrote:

[snip unneeded code]


I am trying to have this SQL statement return a BillingPeriodNumber
and then turn that String value into an Integer. I added the text box
to see the value of strCurPer and now it returns the text of the code
with the dates calculated. What am I doing wrong?

After using the DLookup I suggested in your other thread:

WIth Me.Recordset
If .RecordCount > 0 Then
.FindFirst "BillingPeriodNumber = " & intCurPer
End If
End With

worked like a champ! Many Thanks!
 

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