Solved

VBA error: Expected user-defined type, not project.

Posted on 2003-10-30
9
2,718 Views
Last Modified: 2010-05-18
I'm trying to learn VBA in access and I cut-pasted some sample code from an online book on the subject into my Visual Basic Editor (with my Access database open).

I changed the names in the sample to match my database, but I get this error when I run the code:

Expected user-defined type, not project.

Here's the code:

Sub exaObjectVar()

'Declare some object variables
Dim dbLib As fct
Dim rsFact As Recordset
Dim rsFact2 As Recordset

'Set dbLib to the current database (i.e. fct)
Set dbLib = CurrentDb

'Open a recordset object for the fact table
Set rsFact = dbLib.OpenRecordset("fact")

'Two object variables will refer to the same object
Set rsFact2 = rsFact

'Use a property of this object
MsgBox "fact record count: " & rsFact.RecordCount

'Destroy the object using rsFact2 reference
rsFact2.Close

'Now rsFact has nothing to refer to, so we get error
MsgBox "fact record count: " & rsFact.RecordCount

End Sub

0
Comment
Question by:gateguard
9 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 9649167
Once I got such message in the project with the name like some VB function or keyword. For example, if project name is MyDB, and you write:

dim MyDB as Database
set MyDB = new Database

you get such error. VB is stupid. I don't know whether such situation is possible in VBA, possibly conflict with database name.
0
 
LVL 3

Assisted Solution

by:kenspencer
kenspencer earned 200 total points
ID: 9649456
Hi,
It looks like your problem is in:     Dim dbLib As fct
fct is the unknown type and should be replaced by Database

Ken
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 100 total points
ID: 9649483
what does this line:

Dim dbLib As fct


mean?

what is fct?

You the attempt to set dbLib to be CurrentDb, which REQUIRES that the variable dbLib be declared as a Database type (Dim dbLib as Database).

fct may be the NAME of the Database you are using, but if this is being done in VBA code, in a module within the DAtabase(named 'fct') itself, then CurrentDb IS in fact pointing to the 'current' database (which is the one named 'fct')

you seem to have a mis-understanding of what the Dim statement is used for.

Dim dbLib as Database  declares that the Variable named 'dbLib' is a DATABASE type object (actually, the variable dbLib will hold a REFERENCE to a Datbase type object), but does NOT assign a 'value' to that variable.  You then use the Set dbLib = CurrentDb statement to actually assign the value of dbLib to be a REFERENCE to the Current Database (the database within which the code is executing).

AW
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9649486
so try changin this line:

Dim dbLib As fct


to read

Dim dbLib as Database

and see if that helps.

AW
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Expert Comment

by:clangl
ID: 9649641
Depending on What version of Access you are running it may not like the Database.  You may have to go into the Tools...References Menu and select DAO library.  If you have Both ADO and DAO in the project you will have to explicity dim the objects

Dim db as DAO.Database
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9650323
I am with Arthur:
(what is fct?)

gateguard, some feedaback required.....
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9652406
Richie_Simonetti:  my guess is that gateguard is VERY new to this and has a database named "fct.mdb".  He/she thinks that CurrentDb should be pointed to a variable named fct, to be sure that it points to the correct mdb (of course, this code is writen and is running INSIDE fct.mdb, but clrearly gateguard is VERY confused as to what is going on).

AW
0
 

Author Comment

by:gateguard
ID: 9660381
fct is the name of my database

I see the syntax error in that line.

I'm going to correct it and try again.

(And it's true, I am confused!)
0
 

Author Comment

by:gateguard
ID: 9660403
that was it
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now