Basic programming help

Mcgrco

Registered User.
Local time
Today, 07:57
Joined
Jun 19, 2001
Messages
118
I Have 2 controls on a form . One is an option box and the other is a command button.
on the command on click comand i have put a simple if statement that says if the option box is ticked, open the report print preview else print the report.

When i run itIt gives me the error "object doesnt support this property or method"

Can any one tell me where im going wrong
Much appriciated.

Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
Dim stDocName As String
If Me![Option1].Selection = True Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acPreview
' End If

Else
If Me![Button136].Selection = False Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acNormal, "", ""

End If
End If


Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click
 
Hi,

i think you just need to lose the .selection from line like Me![Button136].Selection . What you're after testing is the value of the check box rather than anything else. Leaving it as me![Button136] shoule work as it's basicaly a shorthand way of putting Me![Button136].Value

HTH

Drew
 
That worked.

Thanks allot for you help Drew.
 
Hi Mcgrco

I see you have already received advice and sorted out your problem, so perhaps this posting is pointless. However, I would like to make a suggestion or two about tidying up your coding and naming the objects more consistently.

At the moment you have:

--------------------------------------------
Private Sub Command0_Click()
On Error GoTo Err_Command0_Click
Dim stDocName As String
If Me![Option1].Selection = True Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acPreview
' End If

Else
If Me![Button136] = False Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acNormal, "", ""

End If
End If


Exit_Command0_Click:
Exit Sub

Err_Command0_Click:
MsgBox Err.Description
Resume Exit_Command0_Click
------------------------------------------


Access automatically names things for users/developers, which is helpful to a degree, as the names given do at least indicate the type of control - Command (for command buttons) Text (for text boxes) and so on. However, anyone looking at the code who didn't write it or even the author, looking back at code written some weeks/months before, will find the rather bland names of controls hard to interpret (particularly if there are a number of controls/code events).

So most developers use a naming convention which combines a prefix (indicating the type of control) with a brief but informative description. No blanks or spaces are used, the prefix starts with a lower case letter and the words within the description all start with capitals. Examples of prefixes, which are usually are cmd - Command Button, chk - Check box, txt for Text box etc. So Command0 becomes cmdPrintEquityFutures (for example). This then means changing Command0 all the way through to the new name (but this can be easily done by using Edit and replace on the Menu Bar).
---------------------------------------------
Private Sub cmdPrintEquityFutures_Click()
On Error GoTo Err_cmdPrintEquityFutures_Click
Dim stDocName As String
If Me![Option1].Selection = True Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acPreview
' End If

Else
If Me![Button136] = False Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acNormal, "", ""

End If
End If


Exit_cmdPrintEquityFutures_Click:
Exit Sub

Err_cmdPrintEquityFutures_Click:
MsgBox Err.Description
Resume Exit_cmdPrintEquityFutures_Click
-------------------------------------------

The next name that slightly confuses me is Me![Option1] - is this an option button as part of a group or a check box (where a tick can be placed)? I will assume it is a check box (as a series of options within an option group would not fit in with the description of two controls on the form). So this control could be called chkPreview (as when it is selected the report is previewed). The next change is to the control called Button136 - presumably this is something different from chkPreview (what was - Me![Option1]), but there are only two controls on the form. This may have been part of the original problem, but I actually think this section of the coding is unnecessary as the If codition checks if the Preview choice is selected. If it isn't, then the coding following the Else is automatically implemented, so I think the coding could be refined to this:

---------------------------------------------
Private Sub cmdPrintEquityFutures_Click()
On Error GoTo Err_cmdPrintEquityFutures_Click
Dim stDocName As String
If Me![chkPreview] = True Then

stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acPreview


Else
stDocName = "Morgan Grenfell and Co Ltd - Equity Futures"
DoCmd.OpenReport stDocName, acNormal, "", ""

End If

Exit_cmdPrintEquityFutures_Click:
Exit Sub

Err_cmdPrintEquityFutures_Click:
MsgBox Err.Description
Resume Exit_cmdPrintEquityFutures_Click
-------------------------------------------

(I have also implemented the suggested dropping of .Selection for the reasons given by KDg)

Finally, some fine tuning:
*it is possible to only refer to the actual report once - before the If condition,
*the extra "","" after the command to print directly is unnecessary
*I prefer to use str rather than st as the prefix for a String (I know this can be seen as being very picky/pedantic ... but if you follow a naming convention you ought to be consistent!).

This results in:

---------------------------------------------
Private Sub cmdPrintEquityFutures_Click()

On Error GoTo Err_cmdPrintEquityFutures_Click

Dim strDocName As String
strDocName = "Morgan Grenfell and Co Ltd - Equity Futures"

If Me![chkPreview] = True Then
DoCmd.OpenReport strDocName, acPreview

Else
DoCmd.OpenReport strDocName, acNormal

End If

Exit_cmdPrintEquityFutures_Click:
Exit Sub

Err_cmdPrintEquityFutures_Click:
MsgBox Err.Description
Resume Exit_cmdPrintEquityFutures_Click
-------------------------------------------

There are one or two other possible changes - the name of the report doesn't at the moment follow a naming convention - instead of "Morgan Grenfell and Co Ltd - Equity Futures" I would have thought "rptEquityFutures" is all that is needed (the name of the company is quite long and not something, presumably, that distinguishes one report within this database from another). Also it is possible to add more helpful information in the Error trapping (but that is another story!).

HTH

Rich Gorvin

[This message has been edited by Rich@ITTC (edited 07-25-2001).]
 
thanks for your advice. It was very helpfull.
The procedure was a " see if I can do it" senario. Once the core code worked i tweaked the code to use you naming convention
 

Users who are viewing this thread

Back
Top Bottom