Solved

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

Posted on 2003-10-30
9
2,755 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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
 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

770 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