Solved

VBA syntax MS Access

Posted on 2011-09-15
9
311 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

861 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