Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default VBA Database access

Hello

It's been a while since I've been programming in classical VB... (no VB.net)
I now have to write some Excel VBA stuff to access a Database (SQL-Server)
and I don't know how.
Is there any good tutorial or can anybody help me?
Finally, the user must be able to enter a number in the excel sheet and the
macro then gets a record from the database, using the user's input as
WHERE-argument
Thanks for your help!

Simon


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default VBA Database access

Simon,

Here's a simple example of using ADO to access a SQL Server database. You
would need to set all of the connection properties as appropriate, ,and set
the SQL command.

You will also need to set a reference to Microsoft ActiveX Data Objects n.n
Library under Tools/References

Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim iField As Long

Set oConn = New ADODB.Connection
oConn .CursorLocation = adUseClient
oConn .Open "PROVIDER=MSDASQL;driver={SQL Server};" & _
"server=MyServerName;" & _
"uid=MyUID;" & _
"pwd=MyPassword;" & _
"database=MyDatabaseName;"
Set oRS = New ADODB.Recordset
oRS.Open "SELECT * FROM [MyTable] WHERE fieldx = " & myVar, _
oConn, adOpenStatic,adLockOptimistic
ActiveSheet.Cells.Clear
If Not oRS.EOF Then
For iField = 1 To oRS.Fields.Count
Cells(1, iField).Value = oRS.Fields(iField -1).Name
Next
Range("A2").CopyFromRecordset oRS
End If
oRS.Close
Set oRS= Nothing
oConn.Close
Set oConn = Nothing


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Simon Sunke" wrote in message
...
Hello

It's been a while since I've been programming in classical VB... (no

VB.net)
I now have to write some Excel VBA stuff to access a Database (SQL-Server)
and I don't know how.
Is there any good tutorial or can anybody help me?
Finally, the user must be able to enter a number in the excel sheet and

the
macro then gets a record from the database, using the user's input as
WHERE-argument
Thanks for your help!

Simon




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 535
Default VBA Database access

Hi Simon,

It's been a while since I've been programming in classical VB... (no VB.net)
I now have to write some Excel VBA stuff to access a Database (SQL-Server)
and I don't know how.
Is there any good tutorial or can anybody help me?
Finally, the user must be able to enter a number in the excel sheet and the
macro then gets a record from the database, using the user's input as
WHERE-argument


Check out this site:

http://www.erlandsendata.no/english/...odao/index.htm

Regards,

Jan Karel Pieterse
Excel MVP
www.jkp-ads.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10
Default VBA Database access

Thanks a lot, works great!

Simon


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
Update Excel Database via Access Main Database with a script Finnbar New Users to Excel 2 November 3rd 08 07:24 PM
Access Database John Excel Discussion (Misc queries) 0 August 29th 05 07:54 AM
Microsoft Access database Tracy Excel Discussion (Misc queries) 2 August 6th 05 05:22 AM
Open Access Database with VBA Bubba Excel Discussion (Misc queries) 1 July 19th 05 10:53 PM
Open Access Database with VBA Keith Excel Discussion (Misc queries) 1 June 1st 05 07:18 PM


All times are GMT +1. The time now is 11:52 PM.

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"