Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 export data from excel2003 to sql query

Author  Topic 

arieltopaz
Starting Member

2 Posts

Posted - 2008-03-26 : 04:17:13
hi
i have a problem
and i hope u can help me

i want to write sql query into vba
how i do that

(i need to take the data from one cell and put it
in my sql query )

thank u all


jrogers
Starting Member

34 Posts

Posted - 2008-03-26 : 05:13:17
you need to use the com object ADODB
add a reference to MIcrosoft ActiveX Data Objects 2.x Library (2.8 is the latest)
then use the following

Sub Test
Dim cn as ADODB.Connection
Dim rs as ADODB.Recordset
Dim strSQL as string

Set cn = new ADODB.Connection
Set rs = New ADODB.Recordset

cn.ConnectionString = "Provider=sqloledb;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"

strSQL = "Select field1,field2,field3 from table1 where field1 = '" & sheets(1).Range("A6").value & "'"
cn.open
rs.open strsql,cn,adOpenStatic, adLockBatchOptimistic

'Dispose & close objects
rs.close
rs = Nothing
cn.close
cn = nothing
End Sub
Go to Top of Page
   

- Advertisement -