Solved

Basic SQL query from word 2003 (vba)

Posted on 2011-09-02
11
223 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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…
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…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

726 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