UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> My Way To Make A Dialog Box, Any Version    
 
   
mempie
post Nov 18 2019, 02:55 PM
Post#1



Posts: 262
Joined: 27-September 01



I’m interested in expert opinion of my way of making dialog boxes. I dreamed up this way of doing it, without knowing how the experts usually make them. My idea is to encapsulate a process for having a user input or select a value or values, saving those values, and clearing everything else away. I invite our experts to tell me what they think of it.

I start with a function that returns a value, or a function that retrieves multiple values and puts them in arguments ByRef. For instance, if I need a user to pick a name from a People table, I’ll call a function like this to return the person’s ID.
lngPersonINeed = GetPerson().
If I have an existing person to use as a default, I can pass the ID as a parameter:
lngPersonINeed = GetPerson(lngDefaultID)
If the People in the People table fit naturally into certain groups, such as Geographic Region or Employer, I might know the group on which I want to filter the people, or to use as a default filter.
lngPersonINeed = GetPerson(lngDefaultID, txtDefaultGroup)
If I need more than one value, my function might look like this
boolSuccess = GetACertainSetOfValues(txtFirstValue, lngSecondValue, dteThirdValue)
This function returns false if it fails. The parameters are declared ByRef in the function.

The next piece is a global object variable declared wherever I’m declaring variables and things. It might be called objPerson or objCertainValueSet.

The third piece is a class module that holds the needed values and defaults. It has a property for each value needed, and a property for each default filter condition, or anything else that might be imposed in the dialog box. It also have a boolOK property, that is set to True if the user clicked OK in the dialog box to return a value or values. If the user clicked a Cancel button to return no value, boolOK is False. The class module has a LaunchDialogBox method which opens the dialog box form. It probably has a SetDefaults method which sets the dialog box form’s controls to any needed initial values. There is a RetrieveValues method which takes the user’s selections and inputs and puts them in the class module’s properties.

The last piece is a modal, always-on-top form, with controls for selecting a value or values, maybe some filter controls, and OK and Cancel buttons which both close the form. The dialog box’s .Open event looks for the global object variable objValueSet. If it’s there, it calls its .SetDefaults method, passing the form as an argument. The OK button will call the RetrieveValues method of the class (passing the form as an argument), set boolOK to True, and close the dialog box.

My GetPerson or GetACertainSetOfValues function begins by setting the object variable to an instance of my class.

Here’s an example of a possible GetACertainSetOfValues()

Public Function GetACertainSetOfValues (Optional ByRef strFirstValue As String = vbNullString, _
Optional ByRef lngSecondValue As Long = 0, _
Optional ByRef dteThirdValue As Date = #1/1/1900#) As Boolean
Set objValueSet = CreateObject("clsMyDialog")
With objValueSet
.strFirst = strFirstValue
.lngSecond = lngSecondValue
.dteThird = dteThirdValue
.boolOK = False
.LaunchDialogBox
If .boolOK Then
strFirstValue = .strFirst
lngSecondValue = .lngSecond
dteThirdValue = .dteThird
End If
End With

‘ This line is optional
Set objValueSet = Nothing

End function

The last line of the function can Set objValueSet = Nothing if I know I don’t need to keep any dialog box data, and just want to clean up. However, if I want to save the selected values for use as defaults the next time I open the same dialog box, objValueSet can be left alive.

What do people think of my technique? How can it be improved?
Go to the top of the page
 
theDBguy
post Nov 18 2019, 04:58 PM
Post#2


UA Moderator
Posts: 76,885
Joined: 19-June 07
From: SunnySandyEggo


Hi. That's certainly one way of doing it, and I like that you're using a Class to do it. I always say you should go with what is comfortable for you. So, if you don't like hidden forms and prefer to use a class instance, then that's fine. Both techniques work, so either one you choose should be fine. Most of the stuff we do involves personal preferences. Cheers!

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
mempie
post Nov 19 2019, 08:38 AM
Post#3



Posts: 262
Joined: 27-September 01



Your response to my other post mentioned the TempVars collection. Apparently Microsoft added it to Access for version 2007. I never heard about it. My global object variable works well, but it could get stomped by an unhandled error. I use an error handler in my code, but there might be a bit of code somewhere in any given database of mine that neglects to handle errors. Hopefully an unhandled error never happens while one of my dialog boxes is open.

I could consider redesigning my approach, using TempVars instead of the class and the global object variable. Then I'd still need a good place to put the code now held in the methods of the class. Just a group of public functions, with names changed to make them unique across different dialog boxes, would probably suffice.

I read the old Access 2002 Desktop and Enterprise Developer's Handbooks, by Litwin, Getz, and Gunderloy. They were great books, but I don't remember anything in them about dialog box design. I wonder what books, if any, really get into this topic.
Go to the top of the page
 
cheekybuddha
post Nov 19 2019, 09:00 AM
Post#4


UtterAccess Moderator
Posts: 11,910
Joined: 6-December 03
From: Telegraph Hill


>> My global object variable works well, but it could get stomped by an unhandled error. <<

Use a private variable.

Then use a function/Property Let/Get to retrieve/set it.

eg
CODE
Option Explicit
Option Compare Database

Private m_strSomeVal As String

Public Property Get SomeVal() As String

  SomeVal = m_strSomeVal

End Property

Public Property Let SomeVal(strSomeVal As String)

  m_strSomeVal = strSomeVal

End Property


Then an unhandled error will not kill your private variable.

Test in the Immediate Window (Ctrl+G):
CODE
SomeVal = "Hello"
?SomeVal
Hello
?1/0
?SomeVal
Hello


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
mempie
post Nov 19 2019, 02:13 PM
Post#5



Posts: 262
Joined: 27-September 01



The global variable I have been using for my dialog boxes is an object variable. I always had trouble understanding the uses of Property Let and Property Get. I'm trying to understand how I would use them to keep my object, which is an instance of a custom class.
Go to the top of the page
 
cheekybuddha
post Nov 19 2019, 02:34 PM
Post#6


UtterAccess Moderator
Posts: 11,910
Joined: 6-December 03
From: Telegraph Hill


CODE
Option Explicit
Option Compare Database

Private m_objYourClass As Object

Public Property Get SomeObj() As Object

  Set SomeObj = m_objYourClass

End Property

Public Property Set SomeObj(obj As Object)

  Set m_objYourClass = obj

End Property


You may have to specify the actual class name instead of As Object

hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 
cheekybuddha
post Nov 19 2019, 02:37 PM
Post#7


UtterAccess Moderator
Posts: 11,910
Joined: 6-December 03
From: Telegraph Hill


First use:
CODE
  Set SomeObj = New YourClass


Then, just use:
CODE
  SomeObj.Prop1 = "Hello"
  SomeObj.Prop2 = "World!"
  Me.txtExample = SomeObj.EchoProps


hth,

d

--------------------


Regards,

David Marten
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    12th December 2019 - 12:22 PM