Solved

MS Access 2003 Pass Control To Function in Module

Posted on 2012-03-10
7
482 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
[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
  • 3
  • 3
7 Comments
 
LVL 58
ID: 37705363
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 58
ID: 37705367
Also you want to use:

cltDisplay.Value =

or simply

cltDisplay =

Not cltDisplay.text

Jim.
0
 
LVL 48

Expert Comment

by:Martin Liss
ID: 37705419
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 1

Accepted Solution

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

The issue was a typo. case closed.
0
 
LVL 58
ID: 37706993
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
ID: 37707263
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
ID: 37723935
solved myself
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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…
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.
Suggested Courses

630 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