Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Access 2003 Pass Control To Function in Module

Posted on 2012-03-10
7
Medium Priority
?
490 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 49

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

715 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