Solved

MS Access 2003 Pass Control To Function in Module

Posted on 2012-03-10
7
424 Views
Last Modified: 2012-03-15
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?
0
Comment
Question by:jriggin
  • 3
  • 3
7 Comments
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
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.
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
Also you want to use:

cltDisplay.Value =

or simply

cltDisplay =

Not cltDisplay.text

Jim.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Accepted Solution

by:
jriggin earned 0 total points
Comment Utility
Thanks, guys., but I know how to debug VB.

The issue was a typo. case closed.
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
For furture readers of the thread, it would be good if you indicated exactly what the typo was and the fix.

Jim.
0
 
LVL 1

Author Comment

by:jriggin
Comment Utility
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.
0
 
LVL 1

Author Closing Comment

by:jriggin
Comment Utility
solved myself
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now