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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,337
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
If this code is in Excel, you need a reference to the Microsoft Office 16.0 Access Database Engine Object library.
 

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
Thanks that`s stoped that from happening but
Now says on

VBA Code:
 db.OpenRecordset

Type mismatch?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,337
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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)
 

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
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?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,337
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
I have shut down and reopened but no luck?
The line below is the culprit.
VBA Code:
Set rsQry = db.OpenRecordset("Select * [ModelType]", dbOpenSnapshot)
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,337
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
That's not a valid SQL string - you are missing the FROM keyword before the table name.
 

Darren Smith

Well-known Member
Joined
Nov 23, 2020
Messages
631
Office Version
  1. 2019
Platform
  1. Windows
I`ve put the FROM in there but it`s still doing the same
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
39,337
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Are you sure it's on that line? That doesn't really make sense as you've already opened the database.
 

Forum statistics

Threads
1,176,002
Messages
5,900,799
Members
434,854
Latest member
ExcelMuffin

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
Top