Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Basic SQL query from word 2003 (vba)

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
Zoodiaq
Asked:
Zoodiaq
  • 5
  • 3
1 Solution
 
lcohanDatabase AnalystCommented:
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
 
ZoodiaqAuthor Commented:
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
 
Anthony PerkinsCommented:
Are you familiar with ADO?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ZoodiaqAuthor Commented:
Only a bit. I know I need it in the reference in VBA.
0
 
ZoodiaqAuthor Commented:
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
 
Anthony PerkinsCommented:
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
 
ZoodiaqAuthor Commented:
Do I need to connect via ODBC in the controlpanel on the workstation for this to work?
0
 
Anthony PerkinsCommented:
>>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
 
ZoodiaqAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now