Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access 2003 Pass Control To Function in Module

Posted on 2012-03-10
7
Medium Priority
?
497 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

916 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