Solved

VBA syntax MS Access

Posted on 2011-09-15
9
295 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
 

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now