Solved

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

Posted on 2003-10-30
9
2,737 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

18 Experts available now in Live!

Get 1:1 Help Now