[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA populate label from database

Posted on 2006-03-22
5
Medium Priority
?
242 Views
Last Modified: 2010-04-07
Hello, I'm trying to populate a label from the data that is being returned from a SQL query, but keep getting an error: must declare the varible @stringm163

Can someone please look at my code below and show me where I'm going wrong?

Code:
Sub getUser()
Dim conn As ADODB.Connection
Dim connString As String

connString = "Provider=sqloledb;Data Source=localhost;Initial Catalog=test;User Id=scott;Password=rocks"

Set conn = New ADODB.Connection
conn.ConnectionString = connString
conn.Open connString

Dim rs As ADODB.Recordset
Dim sSQL As String

Set rs = New ADODB.Recordset
sSQL = "select username from cstb_sopWindowUsers where username = @StringM163"
rs.Open sSQL, conn

StringM164.Value = rs.Fields.Item("username").Value

rs.Close
conn.Close
Set conn = Nothing
Set rs = Nothing
       
End Sub
0
Comment
Question by:gogetsome
  • 3
  • 2
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 16260751
Is @StringM163 a field name, or a SQL Server parameter?

Field name...
sSQL = "select username from cstb_sopWindowUsers where username=" &  YourFormName.[ @StringM163]

SQL Server parameter...
You'll need an ADODB.Command object, create/set an ADODB.Parameter of @StringM163, then execute your command.
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 16260771
(slight correction, string needs to be surrounded by single tick marks)
sSQL = "select username from cstb_sopWindowUsers where username='" &  YourFormName.[ @StringM163] & "'"  
0
 

Author Comment

by:gogetsome
ID: 16260978
Thank you Jim for responding. I'm trying to obtain the value from a field. I'm using:

sSQL = "select username from cstb_sopWindowUsers where username='" &  SalesTransactionEntry.[ @StringM163] & "'"  
 
but am now getting this error method or data member not found
0
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 16261037
SQL may not allow you to name fields with a punctuation mark as the first character.  

Even if it does, this is highly confusing, as the @ character is usually the prefix for stored procedure variables, and it will force you to use square brackets [ ] to refer to it.

Try renaming your field something meaningful, such as txtUser, txtCustomer, anything instead of @StringM163, and try again.

sSQL = "select username from cstb_sopWindowUsers where username='" &  SalesTransactionEntry.txtUser & "'"  
0
 

Author Comment

by:gogetsome
ID: 16261296
Sorry, but I had the @ character as if I were passing a variable to a SPROC.  I took out the @ and now it works great! Thank you very much for your assistance Jim.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

829 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