473,403 Members | 2,222 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,403 software developers and data experts.

SQL Error

Hi there Ive created some SQL code in the Click event of a submit button. I had it working at the start but when i made a few modifications it keep telling me that and end of statement is required . The strings are retrieved from combo box values and then stored in string variables that are assigned to the SQL string.

Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdSubmit_Click()
  2.  
  3. Dim db As DAO.Database
  4. Dim qdf As DAO.QueryDef
  5. Dim strSQL As String
  6. Dim operatingsystem As String
  7. Dim make As String
  8. Dim computertype As String
  9.  
  10. Set db = CurrentDb
  11. Set qdf = db.QueryDefs("Admin_query")
  12.  
  13. If (Me.CboOS.Value = "All") Then
  14.      operatingsystem = " Like '*' "
  15. Else
  16.  operatingsystem = "='" & Me.CboOS.Value & "' "
  17.  
  18. End If
  19.  
  20. If (Me.CboMake.Value = "All") Then
  21. make = " Like '*' "
  22. Else
  23. make = "='" & Me.CboMake.Value = "='" & "' "
  24.  
  25.  End If
  26.  
  27. 'below is only two of the many SQL variations ive tried
  28.  
  29.  '  strSQL = "SELECT laptops.* " & _
  30.        '     "FROM laptops " & _
  31.        '     "WHERE laptops.operating_sysytem" & operatingsystem & _
  32.        '"AND laptops.manufacturer" & make  ;"
  33.  
  34.       '   strSQL = "SELECT laptops.* " & _
  35.        '  "FROM laptops " & _
  36.        '  "WHERE laptops.[operating_sysytem] = '" & operatingsystem & "' "& _
  37.        '  "AND laptops.[manufacturer]=  '" & make & "' " ;"
  38.  
  39.  
  40.  
  41.          qdf.SQL = strSQL
  42.          DoCmd.OpenQuery "Admin_query"
  43.          DoCmd.Close acForm, Me.Name
  44.         ' MsgBox strSQL
  45.  
  46.  
  47.          Set qdf = Nothing
  48.          Set db = Nothing
  49.          DoCmd.OpenForm "laptop_specs", , "Admin_query
  50.  
  51.         End Sub
  52.  
Can anyone please point me in the right direction
Regards Panteraboy
May 9 '08 #1
3 1372
Its ok folks I got her sorted. SQL is so fidely for a first timer lol
Here is da code if your Interested
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub CmdSubmit_Click()
  3. Dim db As DAO.Database
  4. Dim qdf As DAO.QueryDef
  5. Dim strSQL As String
  6. Dim operatingsystem As String
  7. Dim make As String
  8. Dim computertype As String
  9.  
  10. Set db = CurrentDb
  11. Set qdf = db.QueryDefs("Admin_query")
  12.  
  13. If (Me.CboOS.Value = "All") Then
  14.      operatingsystem = " Like '*' "
  15.  
  16. Else
  17.  operatingsystem = "='" & Me.CboOS.Value & "' "
  18.  
  19.  
  20. End If
  21.  
  22.  
  23. If (Me.CboMake.Value = "All") Then
  24. make = " Like '*' "
  25. Else
  26. make = "='" & Me.CboMake.Value & "' "
  27.  
  28.  End If
  29.  
  30.          strSQL = "SELECT laptops.* " & _
  31.          "FROM laptops " & _
  32.          "WHERE laptops.operating_sysytem" & operatingsystem & _
  33.          "AND laptops.manufacturer" & make & _
  34.          "ORDER BY laptops.model;"
  35.  
  36.          qdf.SQL = strSQL
  37.          'DoCmd.OpenQuery "Admin_query"
  38.          DoCmd.Close acForm, Me.Name
  39.         ' MsgBox strSQL     
  40.  
  41.          Set qdf = Nothing
  42.          Set db = Nothing
  43.          DoCmd.OpenForm "laptop_specs", , "Admin_query"
  44. End Sub
  45.  
Regards Panteraboy
May 9 '08 #2
Stewart Ross
2,545 Expert Mod 2GB
Hi Panteraboy. There is a mistake in line 32 above, which has an extra double quote and semicolon (no doubt from the first version which you altered). Lines 34-37 repeat part of the definition and should be deleted.

]I also think you have misnamed variables make and operatingsystem, which are really the comparison parts of the WHERE clause. Reading the code first time round it looked like you were missing comparison operators at this point, but this is not the case - it is just that the variable names are very misleading

There is also a mistake in the comparison operator itself

You also appear to have no DIM statement for operatingsystem, and if this is the case you will also not have Option Explicit set. This is a crucial setting, as without it you can use undeclared variables without error - including if you simply make a mistake in a variable name. With Option Explicit on, the compiler will not let you use undeclared variables. It is good practice to have this as a default setting.

Another point: whilst it is correct to use the Value property of a control it does not actually add anything to a direct reference to the control itself, which will return the same thing.

A revised version of your code is listed below.

-Stewart

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub CmdSubmit_Click()
  4.  
  5. Dim db As DAO.Database
  6. Dim qdf As DAO.QueryDef
  7. Dim strSQL As String
  8. Dim comparison_for_operatingsystem As String
  9. Dim comparison_for_make As String
  10. Dim computertype As String
  11.  
  12. Set db = CurrentDb
  13. Set qdf = db.QueryDefs("Admin_query")
  14.  
  15. If (Me.CboOS = "All") Then
  16. comparison_for_operatingsystem = " Like '*' "
  17. Else
  18. comparison_for_operatingsystem = "='" & Me.CboOS & "' "
  19. End If
  20.  
  21. If (Me.CboMake.Value = "All") Then
  22. comparison_for_make = " Like '*' "
  23. Else
  24. comparison_for_make = "='" & Me.CboMake & "' "
  25. End If
  26.  
  27. strSQL = "SELECT laptops.* " & _
  28. "FROM laptops " & _
  29. "WHERE laptops.operating_sysytem" & comparison_for_operatingsystem & _
  30. " AND laptops.manufacturer " & comparison_for_make
  31.  
  32.  
  33.  
  34. qdf.SQL = strSQL
  35. DoCmd.OpenQuery "Admin_query"
  36. DoCmd.Close acForm, Me.Name
  37. ' MsgBox strSQL
  38.  
  39.  
  40. Set qdf = Nothing
  41. Set db = Nothing
  42. DoCmd.OpenForm "laptop_specs", , "Admin_query
  43.  
  44. End Sub
  45.  
May 9 '08 #3
Thanks steward for that option Explicit tip. I was not aware of that. Your version is much prettier too lol.
Regards Panteraboy
May 9 '08 #4

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Gregory | last post by:
Hi, One of the disadvantages of using error handling with error codes instead of exception handling is that error codes retuned from a function can be forgotten to check thus leading to...
13
by: deko | last post by:
I use this convention frequently: Exit_Here: Exit Sub HandleErr: Select Case Err.Number Case 3163 Resume Next Case 3376 Resume Next
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
0
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
1
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
0
by: mchuc7719 | last post by:
Hello, I have a Vb.Net 2005 ClassLibrary, when I try to compile using MSBee, only get errors. Before I to run the command line, I open in notepad the .vbproj and I was add the next line: ...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.