Solved

Basic SQL query from word 2003 (vba)

Posted on 2011-09-02
11
221 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
  • 5
  • 3
11 Comments
 
LVL 39

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
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.

 

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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

776 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