Solved

VBA syntax MS Access

Posted on 2011-09-15
9
304 Views
Last Modified: 2012-05-12
I have this code:
Dim Sql As String
Dim Dbs As Database
Set Dbs = CurrentDb
Dim rst As Recordset
Dim Ser As Integer
Ser = Me!SerialNumber

Sql = "SELECT tbl_Inventory.InternalKeyNumber "
Sql = Sql & "FROM tbl_Inventory "
Sql = Sql & "WHERE tbl_Inventory.InternalKeyNumber = Ser"

The criteria is a number but if I print the SQL I get: -
SELECT tbl_Inventory.InternalKeyNumber FROM tbl_Inventory WHERE tbl_Inventory.InternalKeyNumber = Ser

Can anyone help?
0
Comment
Question by:HKFuey
  • 4
  • 4
9 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36542069
Change the last line to this:

Sql = Sql & "WHERE tbl_Inventory.InternalKeyNumber = " & Ser
0
 
LVL 2

Assisted Solution

by:ASP8MBM
ASP8MBM earned 50 total points
ID: 36542070
try this:
Sql = "SELECT tbl_Inventory.InternalKeyNumber "
Sql = Sql & " FROM tbl_Inventory "
Sql = Sql & " WHERE tbl_Inventory.InternalKeyNumber = ' & Ser & "'"

0
 
LVL 61

Expert Comment

by:mbizup
ID: 36542075
In explanation, Ser need to be outside of the quotes and concatenated to the SQL string in order for it's variable value to be used.

If it is inside the quotes, it is treated as literal text.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:HKFuey
ID: 36542101
Thanks for quick response. Variable Ser should be a number so this is still not working for me, I get: -

SELECT tbl_Inventory.InternalKeyNumber FROM tbl_Inventory WHERE tbl_Inventory.InternalKeyNumber =' & Ser & '
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36542109
Did you try my response (first post)?

Sql = Sql & "WHERE tbl_Inventory.InternalKeyNumber = " & Ser
0
 

Author Comment

by:HKFuey
ID: 36542110
I tried dim Ser as integer
Ser = value(Me!Serial)
(Me!serial is Text box on form)
0
 
LVL 61

Accepted Solution

by:
mbizup earned 450 total points
ID: 36542125
Understood...

What are your results when you use this?

Sql = "SELECT tbl_Inventory.InternalKeyNumber "
Sql = Sql & "FROM tbl_Inventory "
Sql = Sql & "WHERE tbl_Inventory.InternalKeyNumber = " & Ser
0
 

Author Comment

by:HKFuey
ID: 36542134
mbizup you get the points, I've got a bit further and now need to log a question re "You mus use dbseechanges with openrecordset when accessing SQL server table that has an identity column"
0
 

Author Closing Comment

by:HKFuey
ID: 36542137
Thanks!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hit enter key to run macro 13 23
Filter a form 8 15
My SQL as Backend for Access 3 18
Switch 5 17
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question