Link to home
Start Free TrialLog in
Avatar of Member_2_241474
Member_2_241474

asked on

MS Access 2003 Pass Control To Function in Module

I've read a dozen posts "pass control to function" but still can't make this work.  

I want a logging function that writes to a log and to a control on the form that called it.
The form has 2 controls -- Text box txtStatus and command button cmdDoSomething

Here's the onclick for cmdDoSomething:

Private Sub cmdDoSomething_Click()
Dim ctlDisplay As Control
Set ctlDisplay = Me.txtStatus
WriteLog "c:\MyLog.txt", "Stuff is happening", "Global", ctlDisplay
End Sub

Open in new window


Here's the function from module1:

Public Function WriteLog(LogSpec As String, LogMsg As String, LogType As String, ByVal ctlDisplay As Control)
Dim fs, ts, strLogText As String
strLogText = Format(Now(), "MMDD:HHNNSS") & " " & LogType & ": " & LogMsg & vbCrLf
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(LogSpec, 8, True, 0)
ts.Write strLogText
ts.Close
Set ts = Nothing
Set fs = Nothing
If Not IsEmpty(cltDisplay) Then ctlDisplay.Text = strLogText & Left(ctlDisplay.Text, 2048)
End Function

Open in new window


No matter what I've tried, ctlDisplay ALWAYS ends up empty.

What am I doing wrong?
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

If Not IsEmpty(cltDisplay)

 If it's NOT empty, then put something in it?

 A few things that will help:

 put a STOP right above that line.  Execute the code.  You'll get the IDE window when you hit that stop.

  Now you can hover over variables with the mouse and see their values or you can call up the debug window (Ctrl/G) and do:

? cltDisplay.Value

or

? cltDisplay.Name to see if the reference is valid and you get back the correct control name.

you can also set the value of anything defined:

strLogText = ""

 Also, now that you've halted execution, pressing F8 will step you through the code line by line so you can see what's getting executed.

 F5 will let execution take off from that point.

HTH,
Jim.
Also you want to use:

cltDisplay.Value =

or simply

cltDisplay =

Not cltDisplay.text

Jim.
JDettman makes some good suggestions about debugging your code. Here's a link to an article I wrote about using the Debug facility. It's aimed at VB6 but most of it should apply to VBA.
ASKER CERTIFIED SOLUTION
Avatar of Member_2_241474
Member_2_241474

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For furture readers of the thread, it would be good if you indicated exactly what the typo was and the fix.

Jim.
Avatar of Member_2_241474
Member_2_241474

ASKER

oops!

definition in declaration:  ByVal ctlDisplay As Control
typo in code If Not IsEmpty(cltDisplay)

Sorry for not noticing my fat fingered code before crying for help - I also forgot to use "option explicit" (which should NOT be optional) in the IDE would have picked it up.
solved myself