473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Incorrect syntax at my strsql coding

Im having problem running the program. Its say i have incorrect syntax at my strsql coding

Expand|Select|Wrap|Line Numbers
  1. Private Sub ComboBox1_Change()
  2.  
  3.     Dim rst As ADODB.Recordset
  4.  
  5.     'Initialize Recordset
  6.     Set rst = New ADODB.Recordset
  7.  
  8.     On Error GoTo errhandle
  9.  
  10.  
  11.     strSq1 = "SELECT DISTINCT activitydesc,materialid FROM activity_table " & _
  12.                 "WHERE  activityid in " & _
  13.                 "(Select distinct activityid from sample where(sampledt BETWEEN StartDt AND EndDT)"
  14.  
  15.         ' Get Start Date & End Date
  16.  
  17.         StartDt = CDate(Format(Range("A4").Value, "dd-MM-yyyy") & " 00:00:00")
  18.         EndDt = CDate(Format(Range("A4").Value, "dd-MM-yyyy") & " 23:59:59")
  19.  
  20.         ' Re-Connect to Database
  21.         MakeConnection
  22.         If Module1.connectSQLSrv(strRegLogin, strRegPwd, strRegDB, strRegServer) Then
  23.             ' Cursor and Lock Characteristic
  24.             ' adOpenDynamic = high Concurrency & Scrollable
  25.             ' adLockOptimistic = other users able to access data
  26.             ' adCmdText = Command Type (Text)
  27.  
  28.             'Open Recordset
  29.             rst.Open strSql, sqlConnection, adOpenDynamic, adLockOptimistic, adCmdText
  30.  
  31.  
  32.             With rst
  33.             If .RecordCount = 0 Then
  34.             ComboBox1.Clear
  35.             ComboBox1.AddItem rst![Request]
  36.         Do While Not .EOF
  37.             If Not IsNull(.Fields(0).Value) Then
  38.             ComboBox1.AddItem (.Fields(0).Value)
  39.             End If
  40.             .MoveNext
  41.         Loop
  42.         Else
  43.     Exit Sub
  44.     End If
  45.     End With
  46.  
  47.     Set rst = Nothing
  48.  
  49.     Else
  50.  
  51.             'Frm Login activate = asking for user login
  52.             'MsgBox "Login Failed, please retry!", vbExclamation
  53.             bolConnected = False
  54.             Module1.MakeConnection
  55.         End If
  56.  
  57.     Exit Sub
  58.  
  59. errhandle:
  60.     MsgBox Err.Number & " : " & Err.Description, vbCritical, "Error!"
  61.     bolConnected = False<<< Having error on this line
  62.     If Not sqlConnection Is Nothing Then
  63.         Set sqlConnection = Nothing
  64.     End If
  65. End Sub
Dec 29 '09 #1
27 3175
debasisdas
8,127 Expert 4TB
May i know why you have posted the code here ?
Dec 29 '09 #2
ThatThatGuy
449 Expert 256MB
now what do i understand by that code.... plz specify the problem youre facing
Dec 29 '09 #3
debasisdas
8,127 Expert 4TB
You need to frame the query string dynamically at run time by concatenating the variables. You need to understand when to use quotes "" and when not . Your variables are within quotes try to fix that. Assign value to the variables first and then concatenate to frame the string.
Dec 30 '09 #4
But the error indicated syntax error near the 'in'

strSql = "SELECT DISTINCT activitydesc,materialid FROM Activity" & _
"WHERE activityid in " & _
"(SELECT DISTINCT activityid FROM sample where(sampledt BETWEEN " & startdt & " AND " & enddt & "))"

this is my new coding
Jan 5 '10 #5
vb5prgrmr
305 Expert 100+
Just jumping in here but there is no space between the word Activity and where in your last post (ActivityWhere) which would be an error near the "in" clause...



Good Luck
Jan 5 '10 #6
sorry can explain again cause im really very new in programming
Jan 5 '10 #7
vb5prgrmr
305 Expert 100+
debug.print strSQL...(look at your string concatenation)



Good Luck
Jan 5 '10 #8
you mean the coding in my strSQL is wrong ? if wrong which part i need to edit ???
Jan 6 '10 #9
vb5prgrmr
305 Expert 100+
Two posts up look between the parens...
Jan 6 '10 #10
what is string concatenation ? U mind helping me edit the coding? Thanks in advance
Jan 6 '10 #11
ThatThatGuy
449 Expert 256MB
@dennis1989
When you join two strings to form a new one is called string concatenation
Jan 6 '10 #12
MikeTheBike
639 Expert 512MB
Hi

Perhaps this is the problem

"(SELECT DISTINCT activityid FROM sample where sampledt BETWEEN #" & startdt & "# AND #" & enddt & "#)"

You need to delimit the dates with the # sign.

Also you need to cosider the date format, if you computer local date setting is not in American format (mm/dd/yy) then you will need to format the date to American the format to return the correct records.


Just a thought !?

MTB
Jan 6 '10 #13
thanks im trying it out now
Jan 7 '10 #14
By the way what does it means to delimit the date ? I have checked that my local setting of the date is correct
Jan 7 '10 #15
hmm i tried to delimit the date but it still have the same error
Incorrect Syntax near the keyword 'in'
Jan 7 '10 #16
vb5prgrmr
305 Expert 100+
Argh! DENNIS!!!! Build your string, ADD THE DEBUG.PRING strSQL... WHAT DOES IT SAY???? Do you see where there MIGHT BE two words without a SPACE between them!!!????
Jan 7 '10 #17
sorry i will try it now .im really newbie in programming
Jan 7 '10 #18
i got error 3001 when i add debug.print. argument are of the wrong type
Jan 7 '10 #19
vb5prgrmr
305 Expert 100+
Okay, then... use a message box to display the string...(then you can make a screen shot by ALT+PrntScrn)



Good Luck
Jan 7 '10 #20
u mean just show u the strsql string or the entire coding ?
Jan 8 '10 #21
vb5prgrmr
305 Expert 100+
Just the string that you pass...
Jan 8 '10 #22
Here is the code you need instead.
On Error GoTo errhandle

' Get Start Date & End Date

StartDt = CDate(Format(Range("A4").Value, "dd-MM-yyyy") & " 00:00:00")
EndDt = CDate(Format(Range("A4").Value, "dd-MM-yyyy") & " 23:59:59")

strSq1 = "SELECT DISTINCT activitydesc,materialid FROM activity_table " & "WHERE activityid in (Select distinct activityid from sample where(sampledt BETWEEN " & StartDt & " AND " & EndDT &")"


' Re-Connect to Database
The issue is specifically with the variables.

1. You need to give them the right values first, -before- you build the SQL string.
2. You SQL string did not break out the variables from the string properly.

When you pass a SQL string to be executed, it passes the entire string as it computes. If you don't break out your variables properly, in this case, your date range variables, it will choke on them.

In this case, your date range was just the WORDS, StartDt and EndDt, when really you wanted the VALUE of StartDt and EndDt respectively.
Jan 8 '10 #23
Thanks for the help black library but i still have that syntax error at the keyword in "in"

strSql = "SELECT DISTINCT activitydesc,materialid FROM activity_table" & "WHERE activityid in " & _
"(SELECT DISTINCT activityid FROM sample where(sampledt BETWEEN " & startdt & " AND " & enddt & ")
Jan 11 '10 #24
i manage to solve the initial problem with ur coding but my coding is have error 31 on this code :

errhandle:
MsgBox Err.Number & " : " & Err.Description, vbCritical, "Error!"
bolConnected = False
If Not sqlConnection Is Nothing Then
Set sqlConnection = Nothing
End If
End Sub
Jan 11 '10 #25
strSql = "SELECT DISTINCT activitydesc,materialid FROM Activity " & _
"WHERE activityid in " & _
"(SELECT DISTINCT activityid FROM sample where(sampledt BETWEEN " & startdt & " AND " & enddt & ")) " & _
"WHERE sampledt BETWEEN CONVERT(DATETIME, '01/18/2010', 102) AND CONVERT(DATETIME, '01/18/2010 23:59:59', 102))"


I revised my code yet i still have syntax error at keywords 23.
Can someone help me?
Thanks in advance
Jan 18 '10 #26
debasisdas
8,127 Expert 4TB
Display the strSql to a message box and post he screen shot.
Jan 18 '10 #27
Some changes to my project now, instead of using the combobox to display the value, i need to display the value in excel cells. Wonder if anyone can help me with the code on my first post. Thanks in advance
Jan 19 '10 #28

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Alan Murrell | last post by:
Hello, One of our web hosting clients is getting the following error when someone tried to log in form their login page: --- ODBC ERROR --- Microsoft OLE DB Provider for ODBC Drivers error...
12
by: ColinWard | last post by:
Hi. I am trying to run the following code when the user clicks a button, but I am getting a syntax error in the SQL. I have a feeling it has to do with brackets. Can anyone help? here is the...
1
by: Andi Plotsky | last post by:
Can someone please tell me - what's the problem with the syntax of the Select portion of the call to the ChangeQueryDef function. My " " marks are off - but I can't figure out where - Private...
3
by: priscilla.jenkins | last post by:
Alright, I'm really new to SQL and VBA and all this, so I might be completely off course...but just tell me. I know C and Assembly, but that doesn't help me much here. I'm trying to create a...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
4
by: Patrick Olurotimi Ige | last post by:
why is this giving me error System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '='.?? my code is in c# strSql = "Update addmessage Set message = '"+null+"' Where ID=" +ID; ...
2
by: isaac2004 | last post by:
hello i am getting a weird al syntax error from my SQL statement Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Syntax error (missing operator) in query expression...
5
by: Mario Krsnic | last post by:
Hallo zusammen, I have an app. with SQL-Server. The same page worked fine with Access-DB. Now I have a problem with this command: Dim strSQL As String Dim myConnection As New...
4
by: nerd4access | last post by:
Hello all! I am not new to access, but new to coding (and posting). I have a database that I have created and need some help with a login form. When a user opens the database, a form pops up...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.