Beginner programming - #Name? error

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m a novice at VB programming. At present I’m working my way through this
Microsoft article:
http://www.microsoft.com/accessdev/articles/bapp97/chapters/ba02_3.htm

(Chapter 2 - Introducing Visual Basic)
(Part 3, Creating your first function)

I’ve set up a practice database.
I created the module: FirstOfNextMonth()
I created a simple Orders table with 4 fields including BillingDate.
I created an Orders form with controls for each field.
In design view I opened the Properties dialog box.
Under ControlSource I deleted BillingDate and typed
=FirstOfNextMonth()
When I switched to form view the control displayed #Name?

As far as I can tell, I simply copied the instructions from the article.
I am completely lost - where have I gone wrong?
 
Peter K said:
I’m a novice at VB programming. At present I’m working my way through this
Microsoft article:
http://www.microsoft.com/accessdev/articles/bapp97/chapters/ba02_3.htm

(Chapter 2 - Introducing Visual Basic)
(Part 3, Creating your first function)

I’ve set up a practice database.
I created the module: FirstOfNextMonth()
I created a simple Orders table with 4 fields including BillingDate.
I created an Orders form with controls for each field.
In design view I opened the Properties dialog box.
Under ControlSource I deleted BillingDate and typed
=FirstOfNextMonth()
When I switched to form view the control displayed #Name?

As far as I can tell, I simply copied the instructions from the article.
I am completely lost - where have I gone wrong?

The first thing I would do is check the spelling.

When you created the new standard module, was it saved with a name like
Module1 ? And the function in it is Public Function FirstOfNextMonth()?
I ask because you said " I created the module: FirstOfNextMonth()".
 
I created the module: FirstOfNextMonth()
....

Under ControlSource I deleted BillingDate and typed
=FirstOfNextMonth()


If the function is called FirstOfNextMonth, the module must be called
something different. It really doesn't matter much what you call it, as
long as it's different from all the functions and procedures, etc.

I call all my modules something like MStringFunctions or MGeneral or
MAPICalls and so on; classes all begin with C, etc.


Hope that helps


Tim F
 
I used to be a maths tutor, and I very quickly learned the difference between
answering by rote and true understanding. So far I feel like I’m still doing
the programming steps by rote, which is why I’ve got stuck. Hopefully the
understanding will grow.

So, to be precise:

My database has one module, named FirstOfNextMonth.
When I click Design from the Database window, the Visual Basic window opens
and shows the following code:

Function FirstOfNextMonth()

FirstOfNextMonth = DateSerial(Year(Now), Month(Now) + 1, 1)

End Function

In the Properties for the form, I used the Builder to enter the
ControlSource expression, and I’ve checked it again, so the spelling is
correct at that point.

Is that enough detail to guide you?
 
Peter K said:
I used to be a maths tutor, and I very quickly learned the difference between
answering by rote and true understanding. So far I feel like I’m still doing
the programming steps by rote, which is why I’ve got stuck. Hopefully the
understanding will grow.

So, to be precise:

My database has one module, named FirstOfNextMonth.
When I click Design from the Database window, the Visual Basic window opens
and shows the following code:

Function FirstOfNextMonth()

FirstOfNextMonth = DateSerial(Year(Now), Month(Now) + 1, 1)

End Function

In the Properties for the form, I used the Builder to enter the
ControlSource expression, and I’ve checked it again, so the spelling is
correct at that point.

Is that enough detail to guide you?


Yep. Since it is not a spelling error, Access is confused on what you want
because there are several objects with the same name.

You could have a form named "FirstOfNextMonth", with a control named
"FirstOfNextMonth", with a control source as a function "=FirstOfNextMonth()"
and the function in a module named "First of NextMonth". If I were then to
tell you "Open FirstOfNextMonth", which one would you look at?

Enter naming conventions.

Everyone has their own way of naming things. One guy I know names the tables
like "Customer_Name_Def" and names the fields in that table with the
"cnd_LName"; he can easily tell which table the field is in.

I use three letter prefixes:

frm for Forms
qry for Queries
rpt for Reports
mod for Modules

Field names in tables are prefixed with the type:
txtLName for text (strings)
sngHrsWorked for single precision numbers
etc.

For Controls I use:

cbo for combo boxes
tb for text boxes
lst for list boxes
sfm for sub-forms
lbl for unattached labels
ub for unbound controls
....


But don't name modules with a Function or Sub name. Use different modules to
group similar types if functions. I have a module named "modDateTime" the
holds several functions that deal with dates/times: FirstDayOfMonth(),
LastDayOfMonth(), CalcAge(), MinDate(), MaxDate(),etc.

So like Tim Ferguson said, rename the module ("modDateTime" would be good or
"Module1" or even "MyFirstModule" <g>) and the function will (should) work.

Sorry to be so long winded, but I thought you might like to know the why and
not just "do this". No one explained it to me, so it took a long time (and
lots of reading - books and MVP postings) to understand.

Also, don't use spaces in object names (especially field names). It only
causes you lots of extra work. Besides, only the programmer sees the object
names and it is easier to work with a field name "CustLastName" or
"Cust_Last_Name " than it is with "Cust Last Name". The sames goes for Form,
Query & Report names. Spaces are really a pain in the %$## (aka bootie).

HTH
 
I've just got back from a few days of other work. No need to apologise.
Your answer is awesome - as was Tim's which I must have missed 1st time
round. I really appreciate all the practical advice I'm getting from this
site - wish I could say so to more people.

Cheers.
 
Back
Top