Solved

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

Posted on 2003-10-30
9
2,795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 105
Hide vba in gp 7 127
How to set the row selection as it was prior leaving the datagrid in vb6 3 61
checkbox to hide entire section 10 76
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

734 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