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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA SQL statements

Status
Not open for further replies.

scottrod

Technical User
May 24, 2001
21
US
I have a spreadsheet pulling information from an Access database. I use a form to get input from the user, then use the input to build the WHERE part of a SQL query. This is how the query reads:
With oQryTable
.CommandText = Array( _
"SELECT CJE.Department, CJE.Account, CJE.Debits, CJE.Credits, CJE.Journal_Number, CJE.Journal_Period, CJE.Acct_Type" _
& Chr(13) & "" & Chr(10) _
& "FROM `C:\Bogus\Access\CJE`.CJE CJE" _
& Chr(13) & "" & Chr(10) _
& "WHERE " & sWhere _
& Chr(13) & "" & Chr(10) _
, _
"ORDER BY CJE.Journal_Number")

sWhere is built from the form choices.
Range("SQL_WHERE") = "(CJE.Department='" & sDept & "') AND (CJE.Account='" & sAccount & "') AND (CJE.Journal_Period='" & sDate & "')"
When the choice for sDept is 9050.CJE, sDate is APR, and sAccount is AP.2014, the query works. If sAccount is changed to AdminFee.50001000, I receive Run Time Error 13, Type Mismatch. Have I exceeded some sort of length constraint for the statement? It seems if I pick an account with a short name, the query will work. Long names and it bombs.
 
It looks like you have probably dimmed something wrong.

You will have to check how you have dimmed them.

How have you dimmed sAccount?
 
sAccount is dimmed as string. sAccount is populated by choosing an account from a combo box on the user form. Once the selection is made, it is stored on a sheet in a named range. When I need to use it, I set sAccount=Range("cmbAccount"). Since it works with short account names, I thought it might be something where the long account name put me in a situation where the "WHERE" statement exceeded some max length (if there is one).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top