VB How-ToAsk your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
I am a relative beginner to vba programming and am battling with connecting Access to excel using VBA.
My aim is to connect to my excel workbook and set the variable myVar equal to the value in my first cell.
I have been testing the following but cannot get it to work:
Dim cnt As New ADODB.Connection
Dim db As Database
Set db = CurrentDb
cnt.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source= " & db
Set xlApp = GetObject(, "Excel.Application")
Set xlWb = GetObject("D:\DataPreparation\MetaData\Countries.x slx")
Set xlWs = xlWb.Worksheets("Sheet1")
myVar = xlWs.Cells(1, 1).Value
I get a type mismatch error at the Data source line.
I'm not sure if I'm going about this in the right way. I would appreciate it if someone could help me alter the above. If not please can you reccomend a new approach that would work.
db is an object, of type Database. You cannot join an object to a string unless the object has a default property of type String (or another type than is automatically converted to a String, like an Integer, or a Single).