Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Query from ms sql server question


I am new to this. I hope someone here with the expertise can help
creating a syntax to perform a query ms sql server in Excel . For
example, in my worksheet, there a cell which user will enter the
production number and macro will perform an automatic connection to sql
server and query the table base on the production number and retrieve
the data base on the production alone and place it on the excel sheet.
I don't have background in VB code. So please provide me with the syntax
that I need to put in. Here's what I want:
Step 1:
create a connection to sql server (I have server name and database name
and user ID and password) .
Step 2:
perform sql query statement(which I can look up in the MS query window)
based on whatever production number that use input in cell A1 for
example.
Step 3:
return the query result data starting in cell A2
Step 4:
close the connection.


Please give the syntax for every steps and where should I put all the
code in worksheet vba screen.

Thanks!


--
canix
------------------------------------------------------------------------
canix's Profile: http://www.excelforum.com/member.php...o&userid=25962
View this thread: http://www.excelforum.com/showthread...hreadid=394289

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Query from ms sql server question


I tried it but got error at .CommandText ....Please help....

Set cn = New ADODB.Connection
With cn
..ConnectionString = "Provider=sqloledb;" & _
"Data Source=myServer;" & _
"Initial Catalog=myDatabase;" & _
"User Id=myUsername;" & _
"Password=myPassword"
..CursorLocation = adUseClient
..Open
End With
Set cd = New ADODB.Command
With cd
Set .ActiveConnection = cn
..CommandType = adCmdText
..CommandText = "SELECT * " & _ I got
problem when it get to here"FROM dbo.mytable m WITH (NOLOCK) " & _
"WHERE m.ID=" & Range("A1").Value
Set rs = .Execute
End With
Range("A2").CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set cd = Nothing
cn.Close
Set cn = Nothing
End Sub


--
canix
------------------------------------------------------------------------
canix's Profile: http://www.excelforum.com/member.php...o&userid=25962
View this thread: http://www.excelforum.com/showthread...hreadid=394289

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Query from ms sql server question

Hi canix,

CommandType and CommandText should have dots (.) in front of them, since
you're inside the With block. See if that fixes the problem.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


canix wrote:
I tried it but got error at .CommandText ....Please help....

Set cn = New ADODB.Connection
With cn
ConnectionString = "Provider=sqloledb;" & _
"Data Source=myServer;" & _
"Initial Catalog=myDatabase;" & _
"User Id=myUsername;" & _
"Password=myPassword"
CursorLocation = adUseClient
Open
End With
Set cd = New ADODB.Command
With cd
Set .ActiveConnection = cn
CommandType = adCmdText
CommandText = "SELECT * " & _ I got
problem when it get to here"FROM dbo.mytable m WITH (NOLOCK) " & _
"WHERE m.ID=" & Range("A1").Value
Set rs = .Execute
End With
Range("A2").CopyFromRecordset rs
rs.Close
Set rs = Nothing
Set cd = Nothing
cn.Close
Set cn = Nothing
End Sub


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel with SQL Query (*.sql) and SQL Server Kode Excel Discussion (Misc queries) 2 May 8th 07 01:10 PM
Query SQL Server from Excel Doctorjones_md Excel Worksheet Functions 1 January 6th 07 08:35 AM
How do I run a SQL query against a MS SQL Server database from exc DPL Excel Discussion (Misc queries) 0 February 8th 05 07:33 PM
Query SQL Server Kevin L Excel Programming 1 November 30th 04 09:11 PM


All times are GMT +1. The time now is 11:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"