Solved

Basic SQL query from word 2003 (vba)

Posted on 2011-09-02
11
222 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…

790 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