Using Access Query w/Variable

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
I have an access 2007 database that has a handful of queries that are date range driven. Currently, I have to open the db and put the dates in a form...then the queries provide me the data for that data range.

Is it possible to use excel to connect to the access db and set those dates via a cell or variable in excel...therefore filling different tabs with the query data?:confused:
 
Yes, the connection string is the issue.

Where you create the connection, use this instead:

Code:
    With cnn
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .Open MyConn
    End With

Denis
 
Upvote 0
When the code now creates the Access 2007 database, is it possible for it to change the setting(s) to have the tabbed documents (not overlapping) and "Compact on Close"?
 
Upvote 0
Undoubtedly, but I have never bothered. I set that up manually because it's once only for the life of the database.

Denis
 
Upvote 0
OK...so I'm diving in. Here's my code...

Code:
Const TARGET_DB = "Data.accdb"
Sub CreateDB_And_Table()
  
    Dim cat As ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim sDB_Path As String
    
    sDB_Path = ActiveWorkbook.Path & Application.PathSeparator & TARGET_DB
    
    'delete the DB if it already exists
    On Error Resume Next
    Kill sDB_Path
    On Error GoTo 0
    
    'create the new database
    Set cat = New ADOX.Catalog
    cat.Create _
        "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & sDB_Path & ";"
    
    'create the table
    Set tbl = New ADOX.Table
    tbl.Name = "tblData"
    tbl.Columns.Append "Column_01", adDouble
    tbl.Columns.Append "Column_02", adVarWChar, 60
    tbl.Columns.Append "Column_03", adVarChar, 60
    tbl.Columns.Append "Column_04", adVarWChar, 25
    tbl.Columns.Append "Column_05", adDate
    tbl.Columns.Append "Column_06", adDate
    tbl.Columns.Append "Column_07", adDate
    tbl.Columns.Append "Column_08", adWChar, 9
    tbl.Columns.Append "Column_09", adCurrency
    tbl.Columns.Append "Column_10", adDate
    tbl.Columns.Append "Column_11", adDate
    tbl.Columns.Append "Column_12", adDate
    tbl.Columns.Append "Column_13", adCurrency
    tbl.Columns.Append "Column_14", adInteger
    tbl.Columns.Append "Column_15", adCurrency
    cat.Tables.Append tbl
    
    Set cat = Nothing
    
    'now create the primary key
    Call CreatePrimaryKey("tblData", "Column_01")
End Sub

The code bombs on "cat.Tables.Append tbl" with a run-time error saying "Type is invalid."

For my references, I have...

  1. Visual Basic For Applications
  2. Microsoft Excel 12.0 Object Library
  3. OLE Automation
  4. Microsoft Office 12.0 Object Library
  5. Microsoft ActiveX Data Objects 2.8 Library
  6. Microsoft ADO Ext. 2.8 for DDL and Security.
Any ideas/changes? :confused:
 
Upvote 0
Code:
    tbl.Columns.Append "Column_03", adVarChar, 60

Amend to:
Code:
    tbl.Columns.Append "Column_03", [COLOR="Red"]adVarWChar[/COLOR], 60

See:
http://support.microsoft.com/kb/266302

I found this by just typing in the error number in google.
 
Upvote 0
I'm assuming that I can skip the Primary key portion, as my data could have duplicate values in Column 1.

Also, this section of code crashes when the cell is empty. How can I get this to leave the Access field as Null?

Code:
    For i = 2 To Rw
        rst.AddNew
        For j = 1 To 15
            rst(Cells(1, j).Value) = Cells(i, j).Value
        Next j
        rst.Update
    Next i
 
Upvote 0
Are you working on SydneyGeek's tutorial or your original database? You are losing me going back and forth between these things.

I'd recommend go through the tutorial first. Learn how it works. Then apply the lesson to your database.

Re, the empty cells:
For i = 2 To Rw
rst.AddNew
For j = 1 To 15
If Len(Cells(1,J).value > 0 then
rst(Cells(1, j).Value) = Cells(i, j).Value
End If
Next j
rst.Update
Next i
 
Upvote 0
Are you working on SydneyGeek's tutorial or your original database? You are losing me going back and forth between these things.

I'd recommend go through the tutorial first. Learn how it works. Then apply the lesson to your database.

Re, the empty cells:
For i = 2 To Rw
rst.AddNew
For j = 1 To 15
If Len(Cells(1,J).value > 0 then
rst(Cells(1, j).Value) = Cells(i, j).Value
End If
Next j
rst.Update
Next i

Sorry, I'm using the tutorial and using the code to populate access with my data. :(
 
Upvote 0
Maybe just follow the tutorial as is? It comes with a sample file of data so you shouldn't have any problems. Just follow along and learn how it works... then come back to your database to apply the lessons learned. Easier.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top