Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How To Execute A Select Query With VBA In Excel 2003

Posted on 2009-07-02
9
Medium Priority
?
740 Views
Last Modified: 2013-11-27
I have the following query in MS Excel VBA 2003.  I keep getting an error message saying cannot execute a SELECT query.  How can I get around this?
Sql = "SELECT Username, Password, ReleaserStatus " & _
             "FROM UserTable " & _
             "WHERE Username = ' " & Sheets("MainPage").txtUName.Value & " ' "
 
db.Execute Sql

Open in new window

0
Comment
Question by:rgrguric
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 1000 total points
ID: 24764308
Are you trying to get the query results into a recordset?

Dim rs As DAO.Recordset
Set rs = db.OpenRecordset(Sql)
0
 
LVL 42

Expert Comment

by:dqmq
ID: 24764417
You can't execute a Select query that way because there is nowhere for the output to go.  You need to put the select query inside some sort of object that "knows" how to manage the output.  

What do you want to do with the result set from the select?

0
 
LVL 59

Expert Comment

by:Saurabh Singh Teotia
ID: 24764450
Did you meant this..?
Saurabh...

Sql = "SELECT Username, Password, ReleaserStatus " & _
             "FROM UserTable " & _
             "WHERE Username =  """ & Sheets("MainPage").txtUName.Value & """;"
             
 
Db.Execute Sql

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rgrguric
ID: 24764469
Here is the rest of the code.
    Dim db As DAO.Database
    Dim strSQL As String, dbPath As String, var, td As DAO.tabledef
    Sheets("MainPage").Activate
    
    dbPath = "C:\DB.mdb"
    Set db = OpenDatabase(dbPath)
    Sql = "SELECT Username, Password, ReleaserStatus " & _
             "FROM UserTable " & _
             "WHERE Username = ' " & Sheets("MainPage").txtUName.Value & " '"
 
    db.Execute Sql

Open in new window

0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 24764500
Where is UserTable stored, is it in an external database???

Try using the   Data ----> Get External Data ----> New Database Query   from the menu in Excel.

ET
0
 

Author Comment

by:rgrguric
ID: 24764540
The UserTable is stored in an MS Access Database


0
 
LVL 19

Expert Comment

by:Eric Sherman
ID: 24764646
>>>>The UserTable is stored in an MS Access Database<<<<<<<

From the top menu in Excel ...

1.) Data ----> Get External Data ----> New Database Query
2.) When the Choose Data Source window opens, select MS Access Database*
3.) Navigate to your database and follow the remaining prompts.


ET  
0
 
LVL 42

Expert Comment

by:dqmq
ID: 24764748
Even if that code SELECT were to run, it doesn't accomplish anything because you are not doing anything with the result set.  I infer it is intended to return a password and release status, but then what?

What action do you want if 0 rows are returned?
What action do you want if 1 row is returned?
What action do  you want if >1 row are returned?

Please explain what you want to do with the result set from the select.
0
 

Author Comment

by:rgrguric
ID: 24764806
Right now I'm just trying to open the connection to the database and run a query on the table UserTable to ensure it works.  Setting it up as a recordset appears to be the way to go.
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim Sql As String, dbPath As String
    
    dbPath = "C:\DB.mdbdb"
    Sheets("MainPage").Activate
    
    Set db = OpenDatabase(dbPath)
    
    Sql = "SELECT Username, Password, ReleaserStatus " & _
             "FROM UserTable"
    
    Set rs = db.OpenRecordset(Sql)
    rs.MoveFirst
        
    MsgBox (rs![UserName] & " " & rs![Password] & " " & rs![ReleaserStatus])

Open in new window

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

721 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