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

VBA SQL Where statement syntax

Hello,

I have been trying to solve this issue for long and need help from you guys!
Here is the point:
Looking to get the following SQL into VBA:

SELECT Securities.Name, Securities.ISIN
FROM Securities
WHERE (((Securities.Name) Like [Forms]![Securities Search tool]![namecombo] & "*"))

Apparently my WHERE condition syntax is wrong:
Expand|Select|Wrap|Line Numbers
  1. "WHERE (([name] Like " Me.namecombo " & "*" & ")"
"Namecombo" is the name of a combo box

PLease help debug!
Thanks!!!
Dec 16 '06 #1
10 7863
MMcCarthy
14,534 Expert Mod 8TB
Try this ...

Expand|Select|Wrap|Line Numbers
  1. "WHERE (([Securities].[Name]) Like '" & Me.namecombo  & "*" & "')"
  2.  
Mary
Dec 16 '06 #2
NeoPa
32,556 Expert Mod 16PB
Maybe Evans has a point about all that whiskey!
Try :
Expand|Select|Wrap|Line Numbers
  1. "WHERE ([Name] Like '" & Me.namecombo  & "*')"
(a small slip in truth.)
You can add [Securities]. before [Name] too if you like. It is optional in this context.
Dec 16 '06 #3
MMcCarthy
14,534 Expert Mod 8TB
Maybe Evans has a point about all that whiskey!
Try :
Expand|Select|Wrap|Line Numbers
  1. "WHERE ([Name] Like '" & Me.namecombo  & "*')"
(a small slip in truth.)
You can add [Securities]. before [Name] too if you like. It is optional in this context.
Now Ade

It would have worked anyway

Mary
Dec 16 '06 #4
NeoPa
32,556 Expert Mod 16PB
Now Ade

It would have worked anyway

Mary
I know :(.
I was being fussy - but it was fun :)
Dec 16 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
I know :(.
I was being fussy - but it was fun :)
go get ready to go out and stop checking up on me. ;)

Mary
Dec 16 '06 #6
Thanks for help.
Now you can enjoy the week-end.
Here is the code I used to requery the dropdown list of a combo while typing:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Setnamecbo()
  2.     Dim strcboSQL As String
  3.     Dim strWhere As String
  4.     Dim itemVal As Variant
  5.     Dim accListcbo() As Variant
  6.     Dim lngLen As Long
  7.     Dim msg As String
  8.  
  9.  
  10.     strcboSQL = ""
  11. [namecombo].SetFocus
  12.  
  13. strcboSQL = "SELECT [Name], [Coupon rate], [Maturity DD/MM], [Curr], [Type], [ISIN], [Bloomberg]" & vbCrLf & _
  14.             "FROM [Securities]" & vbCrLf & _
  15.             "WHERE ([Name] Like '" & "*" & Me.namecombo.Text & "*')"
  16.         With Me.Typelist
  17.         For Each itemVal In Me.Typelist.ItemsSelected
  18.         If Not IsNull(itemVal) Then
  19.         strWhere = strWhere & """" & .ItemData(itemVal) & """, "
  20.         End If
  21.         Next
  22.         End With
  23.         lngLen = Len(strWhere) - 2 'Without trailing comma and space.
  24.         If lngLen > 0 Then
  25.         strWhere = "[type] IN (" & Left$(strWhere, lngLen) & ")"
  26.         strcboSQL = strcboSQL & " AND " & strWhere
  27.         End If
  28.  
  29.  
  30.     strcboSQL = (strcboSQL & vbCrLf & "ORDER BY [name];")
  31.     'msg = MsgBox(strcboSQL, vbInformation)
  32.  
  33.  
  34.     Me.namecombo.RowSource = strcboSQL
  35.  
  36. End Sub
  37.  
  38.  
Dec 17 '06 #7
MMcCarthy
14,534 Expert Mod 8TB
I'm glad you got it working. Now I'm going to take a leaf out of Adrians book and be a little pedantic. :D

You don't need to use the vbCrLf command in any of the query.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Setnamecbo()
  2.     Dim strcboSQL As String
  3.     Dim strWhere As String
  4.     Dim itemVal As Variant
  5.     Dim accListcbo() As Variant
  6.     Dim lngLen As Long
  7.     Dim msg As String
  8.  
  9.    strcboSQL = ""
  10. [namecombo].SetFocus
  11.  
  12. strcboSQL = "SELECT [Name], [Coupon rate], [Maturity DD/MM], [Curr], [Type], [ISIN], [Bloomberg]" & _
  13.             "FROM [Securities]" & _
  14.             "WHERE ([Name] Like '*" & Me.namecombo.Text & "*')"
  15.         With Me.Typelist
  16.         For Each itemVal In Me.Typelist.ItemsSelected
  17.    If Not IsNull(itemVal) Then
  18.       strWhere = strWhere & "'" & .ItemData(itemVal) & "', "
  19.    End If
  20.         Next
  21.         End With
  22.         lngLen = Len(strWhere) - 2 'Without trailing comma and space.
  23.         If lngLen > 0 Then
  24.         strWhere = "[type] IN (" & Left$(strWhere, lngLen) & ")"
  25.         strcboSQL = strcboSQL & " AND " & strWhere
  26.         End If
  27.  
  28.         strcboSQL = (strcboSQL & "ORDER BY [name];")
  29.     'msg = MsgBox(strcboSQL, vbInformation)
  30.  
  31.                  Me.namecombo.RowSource = strcboSQL
  32.  
  33. End Sub
  34.  
  35.  
Mary
Dec 17 '06 #8
NeoPa
32,556 Expert Mod 16PB
This sounds like I'm posting for the sake of it but I'm really not.
I've seen this happen before ...
Mary is right about the VbCrLf but you should also understand that while SQL interprets any white-space (Characters which don't produce any characters printed but simply reposition where the next character would be printed) equally, and multiple white-space characters qualify the same as a single one, An example of a white-space character should be used to separate the items previously separated by the VbCrLf.
That's the long version for anyone who's curious, the short version is 'Don't forget to replace any VbCrLfs with tab or space characters if you remove them'.
Dec 17 '06 #9
thank you guys for this great "cursus"
I feel like a genius at VB now :)
Dec 20 '06 #10
MMcCarthy
14,534 Expert Mod 8TB
thank you guys for this great "cursus"
I feel like a genius at VB now :)
Try answering a few simple questions here. You may surprise yourself.

Mary
Dec 20 '06 #11

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

Similar topics

47
by: Andrey Tatarinov | last post by:
Hi. It would be great to be able to reverse usage/definition parts in haskell-way with "where" keyword. Since Python 3 would miss lambda, that would be extremly useful for creating readable...
6
by: HeadScratcher | last post by:
I am trying to speed up my update statements by removing inner select statements. Example: update orders set shipname = (select contactName from customers where customerid = orders.customerID)...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
13
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){},...
37
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
23
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.