Excel VBA: Using cell value in SQL where statement - vba

I would like to use 2 cell values as dates in an SQL date range.
I tried the following but it does not work ...
Sql = Sql & "WHERE trunc(dh.actshpdate) between " & Worksheets("Source Data").Range("K2").Value & " and " & Worksheets("Source Data").Range("K3").Value & " "
... can anyone advise how to amend this code?
Thanks, SMORF

I worked it out ...
Sql = Sql & "WHERE trunc(dh.actshpdate) between '" & Worksheets("Source Data").Range("K2").Value & "' and '" & Worksheets("Source Data").Range("K3").Value & "' "

Related

How to resolve error in MS Access - VBA SQL execution?

I want to run the below query through Access-VBA and I receive the error in the attached screen. Any ideas how to solve it?
deletenew = ("DELETE * FROM TEST1 where [ADAM] = " & luadam & " and [Last_Update_Date]=" & " " & "")
DoCmd.RunSQL deletenew
Thanks in advance,
Andreas
Your query is not properly written.
This:
"where [ADAM] = " & luadam & " and [Last_Update_Date]=" & " <<MissingValue>> " & ""
where <<MissingValue>> is not valid date. MS Access database can NOT translate " " into date.
should be replaced with:
"where [ADAM] = '" & TextValueHere & "' and [Last_Update_Date]= #" & ISODateHere & "#"
More at: Examples of using dates as criteria in Access queries

VBA 3131 error in SQL using variables in query

Can you please help me? I was trying to figure it out for about 2 hours but I still have some error in the syntax.
I have the following code with String variables which I need to pass into the SQL query in VBA script, but I am keep getting some syntax errors.
DoCmd.RunSQL "Delete * From " '" & [tableName] & "' & " Where (" '" & [tableFieldName] & "' & " = " & '" & [tableRecord] & "')"
Thank you very much for some advice.
I think you were going for this:
DoCmd.RunSQL "Delete From [" & TableName & "] Where [" & tableFieldName & "] = '" & tableRecord & "'"
Where I assume TableName, tableFieldName and tableRecord are variables??

When I write code inside textbox doesn't accept it

I have a textbox in a form. I use this textbox to write "Codes" and then I save it in the table in the database through the SQL insert statement, but the code doesn't accept to run and gives me an error message:
Run-Time error '3075'.
type of database: Access database
type of field data: LongText
What the problem and how to pass all same problems when I need to save codes inside the database field.
When I try to save the code without (') it's working!
I use this SQL Statement:
CurrentDb.Execute "Update Tbl_Codes Set [LP_ID]= " & Me.txtID & ",
[Code_Title]='" & Me.txtTitle & "'" _
& " ,[Code_Des]= '" & Me.txtDes & "',[Code_Key]= '" & Me.txtKey & "',
[Notes]= '" & Me.txtNotes & "'" _
& " Where [ID]= " & Me.txtID1 & ""
And I want to save this Code:
DSum("Field1";"Table";"Field2= '" & Value & "'")
Please change your code as follows. You need to escape single quotes by doubling them up. A simple replace will work for your.
CurrentDb.Execute "Update Tbl_Codes Set [LP_ID]= " & Replace(Me.txtID,"'","''") & ",
[Code_Title]='" & Replace(Me.txtTitle,"'","''") & "'" _
& " ,[Code_Des]= '" & Replace(Me.txtDes,"'","''") & "',[Code_Key]= '" & Replace(Me.txtKey,"'","''") & "',
[Notes]= '" & Replace(Me.txtNotes,"'","''") & "'" _
& " Where [ID]= " & Me.txtID1 & ""
DSum("Field1";"Table";"Field2= '" & Replace(Value,"'","''") & "'")

MS-Access VBA select query with multiple criteria

I have a dropdown box in an MS Access form which is populated by the following select query:
strSQL = "SELECT [Process] " _
& "FROM [dbo_tbl_Area_Process] " _
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & ";"
Me.Process.RowSource = strSQL
I would like to add Active = -1 as a second criteria to the query to further limit the selections.
I have tried, so far unsuccessfully to add this second criteria and am at a loss as to how to proceed. Any help from the community would be most appreciated.
I have tried the following where conditions:
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & " and Active =-1"
This does not return any results.
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & " and Active ="-1""
This has a compile error:
Expected:end of statement
Following on from mintys comment regarding linked SQL server tables I changed the query to the following:
strSQL = "SELECT dbo_Tbl_Area_Process.Process " _
& "FROM dbo_Tbl_Area_Process " _
& "WHERE Area=" & Chr(34) & Me.Area_NC_Occurred & Chr(34) & " AND Active=True"
Adding the table references to the SELECT and FROM lines gives me the outputs I expected.
Thanks all for your comments

Trouble using variables in VBA SQL WHERE Clause

I am trying to update a table using variables in VBA for Access. The statement is below.
DB.Execute "UPDATE tblSearchersList SET '" & vSearcherDay & "' = " & VHours & "
WHERE Member= '" & Me.cboMember.Column(1) & "'AND [Mission] = '" & Me.Mission & "'"
tblSearcherList is table to update
vSearcherDay is a variable that combines the letter "d" with a number, et(1,2,3,4,5) depending on other query
VHours is a decimal number (number of hours)
Member is a text value from Form Field Me.cboMember.Column(1)
Mission is a text value from form field Me.Mission
I get Runtime error 3061 - Too few parameters expected 2.
Hope I can get some help with this as I have been fighting it for awhile and am losing the battle.
Thanks
New code is this:
Sorry bout the comments thing. I am new and didn't quite know how to do this.
DB.Execute "UPDATE tblSearchersList SET " & vSearcherDay &_
" = " & VHours & " WHERE Member= '" & Me.cboMember.Column(1) & "' &_
" And [Mission] = '" & Me.Mission & "'"
I am quite embarrassed about this but I had the Member field name wrong. Should've been
MemberName instead. I really do appreciate all the quick help I got and will do better next time. It works perfectly. Thank you all.
Don't use apostrophes around field name. Instead
SET '" & vSearcherDay & "' = " &
do
SET " & vSearcherDay & " = " &