Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Format not compiling in VBA for ACCESS 2

Status
Not open for further replies.

jpl458

Technical User
Sep 30, 2009
337
US
The following bit of code is the first part of a long SQL statement to illustrate the problem.

Ballssql = "SELECT dbo_Transaction_Table.Account_Number, dbo_Master_Accounts.FirstName, _"
Ballssql = Ballssql + "dbo_Master_Accounts.LastName, dbo_Transaction_Table.Date_of_Transaction, _"
Ballssql = Ballssql + "Format([dbo_Transaction_Table]![Date_of_Transaction],"ddd") AS Dayofwk, _"

The problem is that the Format function does not compile, and neither do the other format functions in the rest of the sql. I looked and format is supposed to work in VBA, but I must be missing something. Copied the SQL out of the QBE grid.

Using ACCESS 10, Wondows 7 Pro

Thanks in advance

jpl
 
I would start by removing _ from the end of your lines, replace + with the &, and add Spaces at the beginning and at the end (for good measure)

Ballssql = "SELECT dbo_Transaction_Table.Account_Number, dbo_Master_Accounts.FirstName, "
Ballssql = Ballssql & " dbo_Master_Accounts.LastName, dbo_Transaction_Table.Date_of_Transaction, "
Ballssql = Ballssql & " Format([dbo_Transaction_Table]![Date_of_Transaction],"ddd") AS Dayofwk, "

If that doesn't help, check for any Missing References in your Project

Have fun.

---- Andy
 
hi,

1) APOSTROPHY, not QUOTES in the Format() that is in the SQL code, not in your VBA code.
2) No UNDERSCORE @ end of string as each string statement is complete in itself and needs no continuation.

Code:
Ballssql = "SELECT dbo_Transaction_Table.Account_Number, dbo_Master_Accounts.FirstName,"
Ballssql = Ballssql + "dbo_Master_Accounts.LastName, dbo_Transaction_Table.Date_of_Transaction,"
Ballssql = Ballssql + "Format(dbo_Transaction_Table.Date_of_Transaction,'ddd') AS Dayofwk,"

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
My general suggestion for SQL coding in VBA would include...
Code:
    Dim sSQL As String
    
    sSQL = sSQL & "SELECT"
    sSQL = sSQL & "  tt.Account_Number"
    sSQL = sSQL & ", ma.FirstName"
    sSQL = sSQL & ", ma.LastName"
    sSQL = sSQL & ", tt.Date_of_Transaction"
    sSQL = sSQL & ", Format(tt.Date_of_Transaction,'ddd') AS DayOfWk"
    sSQL = sSQL & vbLf
    sSQL = sSQL & "FROM"
    sSQL = sSQL & "  dbo_Transaction_Table tt"
    sSQL = sSQL & ", dbo_Master_Accounts   ma"
    sSQL = sSQL & vbLf
    sSQL = sSQL & "WHERE ..."
Of course the WHERE would define the join of the 2 tables or maybe your SQL uses an INNER JOIN syntax.

This technique assures that the sql string is properly delimited (SPACES/line feed) and makes the code more readable

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks Skip, I knew about the quote thing but didn't see it. I changed the query to the format you suggested and can see how it is easier to work with and make changes to. It all compiles now, lets see if it opens and runs.

May be back to the well on this.

Thanks again

jpl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top