Please can I ask that someone assist with the below SQL code. I am trying to open a recordset based on a variable BUSINESSTERM.
My SQL string looks like this:
sqlstr = "SELECT TblBusinessTerm.BusinessTermID, TblBusinessTerm.BusinessTerm, TblBusinessTerm.BusinessTermDesc, TblBusinessTerm.DomainCatID, " _
& " TblBusinessTerm.BusinessTermLongDesc, TblBusinessTerm.DomainID, TblBusinessSynonym.UpdatedBusinessTerm, * " _
& " FROM TblBusinessTerm LEFT JOIN TblBusinessSynonym ON TblBusinessTerm.BusinessTermID = TblBusinessSynonym.BusinessTermID " _
& " WHERE (TblBusinessTerm.BusinessTermID)= ; " & businessterm
Me.RecordSource = sqlstr
The issue is in the WHERE Statement. I keep on getting the error that I have to many parenthesis, and then when I remove the parenthesis I get an error
Syntax missing operator
Any assistance would be appreciated.
Thank you
Try this:
WHERE TblBusinessTerm.BusinessTermID=" & businessterm
And better:
WHERE TblBusinessTerm.BusinessTermID=" & Nz(businessterm, 0)
Related
I'm using Microsoft Access to develop a database app. An important feature the user would need is to automatically send an email update to all relevant stakeholders.
The problem is that I'm getting
Run-time error '3075' Syntax error in query expression.
Here it is below:
Set rs = db.OpenRecordset("SELECT StakeholderRegister.[StakeholderID], StakeholderRegister.[ProjectID], StakeholderRegister.[FirstName], StakeholderRegister.[LastName], StakeholderRegister.[EmailAddress] " & _
" FROM StakeholderRegister " & _
" WHERE (((StakeholderRegister.[ProjectID]=[Forms]![ChangeLog]![cboProjectID.Value])) ;")
Funny thing is that I created a query table on Access to create the relevant recordset and the turned on SQL view to copy the exact sql string that's above. That query works however it opens an Input Parameter box, whereas this code should be using the value typed into a forms text box as a matching criteria.
To use a variable as a parameter, do not include it within the quotes:
" WHERE StakeholderRegister.[ProjectID]=" & [Forms]![ChangeLog]![cboProjectID].[Value]
or just
" WHERE StakeholderRegister.ProjectID=" & Forms!ChangeLog!cboProjectID.Value
Note: You really only need the square brackets when there is something like a space in the name, which is not the best practice anyway.
I also took the liberty to remove the parentheses, as they are not needed in such a simple WHERE clause, and can cause more trouble than they are worth.
Try,
Dim strSQL As String
strSQL = "SELECT StakeholderRegister.[StakeholderID], StakeholderRegister.[ProjectID], StakeholderRegister.[FirstName], StakeholderRegister.[LastName], StakeholderRegister.[EmailAddress] " & _
" FROM StakeholderRegister " & _
" WHERE StakeholderRegister.[ProjectID]=" & [Forms]![ChangeLog]![cboProjectID].Value & " ;"
Set rs = Db.OpenRecordset(strSQL)
if [ProjectID] field type is text then
Dim strSQL As String
strSQL = "SELECT StakeholderRegister.[StakeholderID], StakeholderRegister.[ProjectID], StakeholderRegister.[FirstName], StakeholderRegister.[LastName], StakeholderRegister.[EmailAddress] " & _
" FROM StakeholderRegister " & _
" WHERE StakeholderRegister.[ProjectID]='" & [Forms]![ChangeLog]![cboProjectID].Value & "' ;"
Set rs = Db.OpenRecordset(strSQL)
I have the following SQL code:
SELECT GrantInformation.GrantRefNumber, GrantInformation.GrantTitle, GrantInformation.StatusGeneral, GrantSummary.Summary
FROM GrantInformation LEFT JOIN GrantSummary ON GrantInformation.GrantRefNumber = GrantSummary.GrantRefNumber
WHERE (((GrantInformation.LeadJointFunder) = "Global Challenges Research Fund")) Or (((GrantInformation.Call) = "AMR large collab"))
GROUP BY GrantInformation.GrantRefNumber, GrantInformation.GrantTitle, GrantInformation.StatusGeneral, GrantSummary.Summary
HAVING (((GrantSummary.Summary) Like ""*" & strsearch & "*"")) OR (((GrantSummary.Summary) Like ""*" & strsearch & "*""));
Which I want to insert into the following VBA:
Private Sub Command12_Click()
strsearch = Me.Text13.Value
Task =
Me.RecordSource = Task
End Sub
After 'Task ='.
However it keeps on returning a compile error, expects end of statement and half the SQL is in red. I have tried adding ' & _' to the end of each line but it still will not compile.
Any suggestions where I am going wrong? Many thanks
You have to put the SQL into a string...
Dim sql As String
sql = "SELECT blah FROM blah;"
Note that this means you have to insert all of the values and double up quotes:
sql = "SELECT blah "
sql = sql & " FROM blah "
sql = sql & " WHERE blah = ""some value"" "
sql = sql & " AND blah = """ & someVariable & """;"
After that, you have to do something with it. For SELECTs, open a recordset:
Dim rs AS DAO.Recordset
Set rs = CurrentDb.OpenRecordset(sql)
Or, for action queries, execute them:
CurrentDb.Execute sql, dbFailOnError
Without knowing how you plan to use it, we can't give much more info than that.
This conversion tool would be quite helpful for automating the process: http://allenbrowne.com/ser-71.html
I suggest to use single quotes inside your SQL string to not mess up the double quotes forming the string.
In my opinion it's a lot better readable than doubled double quotes.
Simplified:
Dim S As String
S = "SELECT foo FROM bar " & _
"WHERE foo = 'Global Challenges Research Fund' " & _
"HAVING (Summary Like '*" & strsearch & "*')"
Note the spaces at the end of each line.
Obligatory reading: How to debug dynamic SQL in VBA
Edit
To simplify handling user entry, I use
' Make a string safe to use in Sql: a'string --> 'a''string'
Public Function Sqlify(ByVal S As String) As String
S = Replace(S, "'", "''")
S = "'" & S & "'"
Sqlify = S
End Function
then it's
"HAVING (Summary Like " & Sqlify("*" & strsearch & "*") & ")"
I'm trying to make a VBA Code to run a query when someone clicks on the button.
It always displays me the error in the title when I run it. Can someone help mes please? I'm thankful for every response.
Option Compare Database
Option Explicit
Sub btnNamebox_Click()
DoCmd.OpenForm "F-Smartphone-Hinzufuegen"
Dim strSQL As String
DoCmd.RunSQL (strSQL)
strSQL = "SELECT Nachname" & _
"FROM tblPersonal " & _
"WHERE Handynummer = " & Forms![F-Smartphone-Hinzufuegen]![FKHandynummer] & ";"
txtnamebox.Value = strSQL
End Sub
Your code makes little sense - you run a SQL statement before you define it, and you can't RunSQL a SELECT statement anyways.
Use DLookup() instead:
S = Nz(DLookup("Nachname", _
"tblPersonal", _
"Handynummer = " & Forms![F-Smartphone-Hinzufuegen]![FKHandynummer]), "")
Ive been trying to get a query I ran in Access to run in VBA but I keep getting errors due to the number of exclamation marks I've been using. The statement I am using is
SQLstat = "SELECT tbl_Date_Check.DateofChecklist, tbl_Tasks.QuestionNumber,tbl_Tasks.Frequency, tbl_Tasks.Questions " _
& "FROM tbl_Tasks, tbl_Date_Check " _
& "WHERE (((tbl_Date_Check.DateofChecklist)=""" & [Forms]![Daily_Checker]![TxtDate] & """) And ((tbl_Tasks.Frequency) = """ & [Forms]![Daily_Checker]![ComFreq]"""))"
Any help would be great thanks
This can possibly be explained by the following SO question: What is the difference between single and double quotes in SQL?
This explains that you need to utilize single quotes '' to surround text in SQL in almost every instance. The fact that you are using double quotes "" may be what is causing the error.
I hope this helps.
-C§
It must read like this for dates:
SQLstat = "SELECT tbl_Date_Check.DateofChecklist, tbl_Tasks.QuestionNumber,tbl_Tasks.Frequency, tbl_Tasks.Questions " _
& "FROM tbl_Tasks, tbl_Date_Check " _
& "WHERE ((tbl_Date_Check.DateofChecklist = #" & Format([Forms]![Daily_Checker]![TxtDate], "yyyy\/mm\/dd") & "#) And (tbl_Tasks.Frequency = " & [Forms]![Daily_Checker]![ComFreq] & "))"
error:
In operator without () in query expression '(EnrollmentsTbl.UserName LIKE ? IN '' [;DATABASE=e:\web\mcfrsitcom0\htdocs\trackingHIPAA\App_Data\subsite.mdb])'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: In operator without () in query expression '(EnrollmentsTbl.UserName LIKE ? IN '' [;DATABASE=e:\web\mcfrsitcom0\htdocs\trackingHIPAA\App_Data\subsite.mdb])'.
not sure why is asking for () in different place?
here is SELECT in ASPX vb.net
SelectCommand="SELECT EnrollmentsTbl.AutoNum, EnrollmentsTbl.UserName, EnrollmentsTbl.SubmitTime, EnrollmentsTbl.ClassName, EnrollmentsTbl.ClassDate, EnrollmentsTbl.ClassTime, EnrollmentsTbl.Enrolled, EnrollmentsTbl.WaitListed, EnrollmentsTbl.Instructor, EnrollmentsTbl.DateCompleted, EnrollmentsTbl.Completed, EnrollmentsTbl.Walkin FROM EnrollmentsTbl WHERE (EnrollmentsTbl.UserName LIKE ? IN '' [;DATABASE=e:\web\mcfrsitcom0\htdocs\trackingHIPAA\App_Data\subsite.mdb])"
From your other comments it sounds like you want to do something more like this:
SelectCommand = _
"SELECT EnrollmentsTbl.AutoNum, EnrollmentsTbl.UserName, EnrollmentsTbl.SubmitTime, " & _
"EnrollmentsTbl.ClassName, EnrollmentsTbl.ClassDate, EnrollmentsTbl.ClassTime, " & _
"EnrollmentsTbl.Enrolled, EnrollmentsTbl.WaitListed, EnrollmentsTbl.Instructor, " & _
"EnrollmentsTbl.DateCompleted, EnrollmentsTbl.Completed, EnrollmentsTbl.Walkin " & _
"FROM EnrollmentsTbl " & _
"WHERE EnrollmentsTbl.UserName IN " & _
"(" & _
"SELECT OtherColumnName " & _
"FROM [;DATABASE=e:\web\mcfrsitcom0\htdocs\trackingHIPAA\App_Data\subsite.mdb].OtherTableName" & _
")"
I just tried a similar query in C# using OleDb and it worked fine.
The IN operator looks for values that exist in a static list of values or the results of a subquery. You seem to be using it to find values in a database file.
You also combine it with the LIKE operator which doesn't make sense.
Perhaps if you explained better what you're trying to do an alternative could be found.