Passing arguments to procedures

P

Peter Hallett

There is something fundamental that I have failed to grasp about passing
arguments to procedures.

I have a form featuring a set of unbound controls displaying dates and the
corresponding days of the week. (For some reason, the long-date format, on
my computer, does not include the latter, as the documentation suggests it
might, so it has to be separately calculated and displayed.)

A default date is displayed in each control, on Form_Open. This can then be
manually adjusted by a pair of ‘up’ and ‘down’ buttons associated with each
day/date control pair. In the case of the control ‘Start_Date’, for example,
the first two procedures are specific to the control, whilst the third Sub
and the following Function are common to the form.

Private Sub ComDateStartNext_Click() ‘Increment date by one day
IncDecDateDay Me.StartDate, Me.StartDay, 1
End Sub

Private Sub ComDateStartPrevious_Click() ‘Decrement date by one day
IncDecDateDay Me.StartDate, Me.StartDay, -1
End Sub

Private Sub IncDecDateDay(InDate, InDay, ByVal Increment As Integer)
InDate = InDate + Increment
InDay = Get_WeekDay(InDate)
End Sub

Private Function Get_WeekDay(InDate) As String
Select Case Weekday(InDate)
Case 1
Get_WeekDay = "SUN"
Case 2
Get_WeekDay = "MON"
Case 3
Get_WeekDay = "TUE"
Case 4
Etc.
End Select
End Function

Nothing happens when the ‘up’ and ‘down’ buttons are clicked but if I
replace the implicit general-purpose Sub IncDecDateDay with the explicit test
version:–

Private Sub IncDecDateDay(InDate, InDay, ByVal Increment As Integer)
Me.StartDate = InDate + Increment
Me.StartDay = Get_WeekDay(InDate)
End Sub

....it all works fine (in the case of the ‘Start_Date’ control, of course)
but my belief that changes to the parameter InDate would be reflected in the
displayed value of the original calling control are clearly wrong. It is not
that I cannot find an alternative solution – I can recast the thing using a
function to return the required values – but what worries me is that I have
obviously been labouring under a misapprehension. Perhaps I spent too long
messing about with ‘C’. Would someone be kind enough to enlighten me?

---Peter Hallett
 
B

Brendan Reynolds

Hi Peter,

Because the data type of your parameters is not specified, there's some
ambiguity about what exactly is being passed to the procedure - a reference
to the control itself, or to the value of the data entered into the control.
What I believe is happening is that it is a reference to the value entered
into the control that is being passed, and this is why the control itself is
not updated. If you change the data type of the parameter to Control or
Textbox, I believe you will see the behavior you expected.

BTW: If the only reason for this is to display the day of the week as well
as the date, I believe you could do that using a custom date format such as
dddd, dd/mm/yyyy (or dddd, mm/dd/yyyy if you are in the US).
 
P

Peter Hallett

Thanks Brendan,

You are absolutely right! All it needed was to change

Private Sub IncDecDateDay(InDate, InDay, ByVal Increment As Integer)
InDate = InDate + Increment
InDay = Get_WeekDay(InDate)
End Sub

...into

Private Sub IncDecDateDay(InDate As Control, InDay As Control, ByVal
Increment As Integer)
InDate = InDate + Increment
InDay = Get_WeekDay(InDate)
End Sub

…and it worked a treat. I am grateful for your assistance. It has saved me
a fair bit of work.

As to date formatting, I am in the UK, where my regional settings appear to
exclude the day-name from the long-date format. I have tried using the
formatting command in form design, but without a lot of luck. Every cloud
has a silver lining, though. A late requirement was that the day-name should
be shown in a larger font than the date and in colour, if possible. Having
provided a separate control by necessity, the change in job spec. was
therefore significantly easier to meet.

Your response has also helped me to answer another of my own questions.
Yes, I obviously did spend too long messing about with ‘C’. Being a
pointer-based language, it tended to instil in one’s mind that passing
parameters by reference simply means passing the address of the control, or
variable. Your response implies that, “It ain’t necessarily so.â€
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top