?
Solved

Basic SQL query from word 2003 (vba)

Posted on 2011-09-02
11
Medium Priority
?
225 Views
Last Modified: 2014-06-27
I just started to use SQL server 2008, and I want to create a simple script in word 2003 (vba) making a query. Eg. I have one table called "People" with one column for first names, one for last names, and one with an unique ID no. I want to make a query for "John Smith" and the result should be the ID no. (Lets say theres only one John Smith.) How do I do that?

Thx. in advance
0
Comment
Question by:Zoodiaq
[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
  • 5
  • 3
11 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 36475921
select id from People where  first_name = 'John' and last_name = 'Smith'

and BTW you can do all these in SQL SSMS - open a "new query" window and type it/execute it from there.
0
 

Author Comment

by:Zoodiaq
ID: 36475996
Could you provide me with the whole sub. Wouldn't I have to define the servername? I need to finnish with the result as a string.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36478209
Are you familiar with ADO?
0
Independent Software Vendors: 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:Zoodiaq
ID: 36478641
Only a bit. I know I need it in the reference in VBA.
0
 

Author Comment

by:Zoodiaq
ID: 36479717
Hi everyone. I would really apreciate some help getting started on this one. I've done quiete a few VBA scripts, so I know the basics, now I just want to know how to pull data from an sql, just a simple script to get me started. I'm rasing the points to 500.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 2000 total points
ID: 36484488
Add a reference to ADO and then use some code like this:
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
With cn
    .ConnectionString = "Your connection string goes here."
    .Open
    
    Set rs = .Execute("SELECT ID FROM People WHERE FirstName = 'John' AND LastName = 'Smith'")
    MsgBox rs.Fields("ID").Value
    rs.Close
    
    Set rs = Nothing
    
    .Close
End With
Set cn = Nothing

Open in new window

0
 

Author Comment

by:Zoodiaq
ID: 36485165
Do I need to connect via ODBC in the controlpanel on the workstation for this to work?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36485321
>>Do I need to connect via ODBC in the controlpanel on the workstation for this to work? <<
That is up to you.  You can, but there is no need.
0
 

Author Comment

by:Zoodiaq
ID: 36519243
Sorry for not getting back. I just put this question on hold, as I ran into another issue connecting, which I need to get solved before I can move on. I added a another question on that issue.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

777 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