Full Version: help with next step, if, setFocus, dont know what to do next?
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
sumhungl0
i have two date boxes on my form that the user gives a date range for a report. (txtDateTo & txtDateFrom) the users can input the dates or press buttons that are predefined.

the buttons that are Last Week & Week Before Last already have a start & stop (mon - fri)

now my problem is for buttons that populate only one field at a time. i have a button for today (current date). now i need to get my field filled by that button. but which one? the one the user pickes. so i used PreviousControl to find last Focus. heres what i did...
CODE
  
If Screen.PreviousControl = Me!txtDateTo Or Me!txtDateFrom Then
    Screen.PreviousControl.SetFocus
    Screen.ActiveControl = Date

so with this code now the user can select the field to fill and then press the button to populate it. that part works good, and thats what i want. now i need to do something with the other part of it. THE ELSE PART. i need an Else to handle the times when focus is not a text box & is not a correct text box. every time another button has the focus when the code is ran, it errors.

im not sure what i need to handle the other input fields on the form and also the buttons when they have focus. anybody know how i can fix this?
pere_de_chipstick
Try (not tested)
CODE
Select Case Screen.PreviousControl.Name
Case "txtDateTo", "txtDateFrom"
    Screen.PreviousControl.SetFocus    
    Screen.ActiveControl = Date
Case Else
   'Do something else
End Select


HTH
sumhungl0
well thats the problem. i dont know what to do for else.

im getting this error message (run-time error '438' object doesnt support this property or method)

that only happens when another button or object on the form has focus when my new button is clicked. so i need to put something in the ELSE to handle this. but what? ive tried lots of things and i cant get it working.

my most recent tests were:
CODE
'Me!txtDateTo.SetFocus
  Me!txtDateTo = Date


but i still get the error. anybody got any ideas?
pere_de_chipstick
Hi,

I am not sure what you want to achieve in the 'Else' part of the code.

You could simply display a message box:
MsgBox "You must select a valid Date field before the date can be entered.", vbInformation

Rather than use a button, you could use the date control's double click event, the control already has the focus hence there is no ambiguity. - A note on the form (e.g. double click date fields to enter today's date) informs the user of the function.

Hope this helps but please post back if I have missed the point.

Edited by: pere_de_chipstick on Sun Aug 3 1:31:01 EDT 2008.
sumhungl0
well im not sure what to do with the else either. thats why im here. i need to do something to handle the error that happens when a button has focus and the date button is pressed. understand?

starts with 2 date fields on my form (txtDateFrom, txtDateTo) (there are others but these are the only ones i want to fill for this report)

i have buttons to fill those fields (lastWeek, twoWeeksAgo, today). the first 2 have double dates so i dont need to know which field to fill. now the (today)button has only one date. i want it to fill the previous focus txtDateFrom or txtDateTo. i have that part working with the code posted eailer^.

heres the problem......
if i press any other button and then press my today button, it errors because the previous focus is a button and not a txt field. i cant figure it out. i need some help. hope that helps explain better.... help?
pere_de_chipstick
Hi

Yes I believe I understand the problem,

Your code needs to determine if the previous control is a valid control and only then should it attempt to do anything.
If its the wrong control then the code should exit with at most a warning messge for the user to select the correct field.
Hence your code sub routine should be no more than this (except perhaps error handling!):
CODE
Private Sub YourButtonName_Click
    Select Case Screen.PreviousControl.Name
    Case "txtDateTo", "txtDateFrom"    
          Screen.PreviousControl.SetFocus        
          Screen.ActiveControl = Date
    Case Else  
        Msgbox  "You must select a valid control before you can enter a date in it ", vbInformation
    End Select
End Sub


Or
CODE
Private Sub YourButtonName_Click
    Select Case Screen.PreviousControl.Name
    Case "txtDateTo"
          Me.txtDateTo = Date
    Case "txtDateFrom"    
          Me.txtDateFrom = Date
    Case Else  
        Msgbox  "You must select a valid control before you can enter a date in it ", vbInformation
    End Select
End Sub


In this code you only do anything if the correct control had the focus immediately previously ,
and gives and warning message if a valid field was not the last field to have the focus.

HTH
sumhungl0
well that second code you posted is working great. i think i was real close to that before with little results. whatever you did is great. i dont understand the difference between case and if but this is working great. thank you so much for the help. sorry for any confusion.
pere_de_chipstick
You're very welcome thumbup.gif

'Select Case' and 'If' are similar, but select case is easier if there are multiple options dependent on the value of a single control or variable; for instance
CODE
IF Me![ControlName] = 1 Then

    'Do Routine 1

Elseif Me![ControlName] = 2 Then

    'Do Routine 2

Elseif Me![ControlName] = 3 Then

    'Do Routine 3

Elseif Me![ControlName] = 4 Then

    'Do Routine 4

End if

could be replaced by
CODE
Select Case Me![ControlName]

Case 1

    'Do Routine 1

Case 2

    'Do Routine 2

Case 3

    'Do Routine 3

Case 4

    'Do Routine 4

End Select


HTH

Edited by: pere_de_chipstick on Mon Aug 4 12:35:59 EDT 2008.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.