Database issue

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
For some reason my

VBA Code:
Dim db as DataBase

says Compile error User-defined type not defined. Does anybody know why it`s driving me mad

VBA Code:
Private Sub Combobox_List()

    Dim rsQry As Recordset
    Dim Qry As String
    Dim i As Long
    Dim db As DataBase
    Dim dbPath As String
    
Qry = "SELECT * FROM VehicleData"
dbPath = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\TGS-SRV01\Share\ShopFloor\PRODUCTION\DLS Cardworker\Access Files\DrNo Data Base.accdb;"
Set db = DBEngine.workspaces(0).OpenDatabase(dbPath)
Set rsQry = db.OpenRecordset(db, dbOpenSnapshot)
Model_Type.Clear

i = 0
While Not rsQry.EOF
'ModelType.AddItem
Me.Model_Type.List(i, 1) = rs.Fields("ModelType").Value
rs.MoveNext
i = i + 1
Wend
rs.Close

   
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
If this code is in Excel, you need a reference to the Microsoft Office 16.0 Access Database Engine Object library.
 
Upvote 0
Thanks that`s stoped that from happening but
Now says on

VBA Code:
 db.OpenRecordset

Type mismatch?
 
Upvote 0
That's because your code makes no sense. ;)

The first argument to OpenRecordset is a SQL string (or the name of a query/table), not a Database object. So it should be something like db.openrecordset("SELECT * from some_Table", dbOpenSnapshot)
 
Upvote 0
Thanks very much for that
Sorry, one more question I seem to have Run-Time error 3051.
This is saying the access database is already open by somebody else but no one else uses this database.
Or it Says you need permission to view and write its data which makes no sense to me?
 
Upvote 0
It's possible your previous attempts have left the database open and locked in a hidden process, so I'd try a restart. If that doesn't work, I need to know where the error occurs.
 
Upvote 0
I have shut down and reopened but no luck?
The line below is the culprit.
VBA Code:
Set rsQry = db.OpenRecordset("Select * [ModelType]", dbOpenSnapshot)
 
Upvote 0
That's not a valid SQL string - you are missing the FROM keyword before the table name.
 
Upvote 0
Are you sure it's on that line? That doesn't really make sense as you've already opened the database.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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