increment an integer variable by 1

G

Gary

I have a strange problem.
I have an option group which consists of two radio buttons.

the option group is tied into a field in my table. and depending on
which button has been pressed the value of the field is either '1' or
'2'.

I want to hide a subform on this form if the value of the field that
the buttons are linked to is '2'. However if the value of the field is
'1' i want to show the subform. I have some code set to run when either
button gets focus. This code checks the value of the field and hides /
unhides the subform accordingly (the code is just below a bit in this
message.)

I have a snag....

It's doing something strange however. the first time either button is
pressed it works fine doing what it should do, however the second and
subsequent times the button is pressed it behaves erraticly and
unreliably. So my solution is to only run the code if it's the first
time the button has been pressed. I thought i'd accomplish this by
putting a bit of code before the code I allready had, as follows.

=================================================================
I put the following code in the 'on open' part of the form.

dim littlefixer as integer
littlefixer = 0
------------------------------------------------------------------

then ... i modified the buttons event code as follows

' added code
littlefixer = littlefixer + 1
If littlefixer <> "1" Then End
Else

'my original code

If Me![User Type].Value = "2" Then Me.[BUSINESS_DETAILS].Visible = True
If Me![User Type].Value = "1" Then Me.[BUSINESS_DETAILS].Visible =
False
End Sub

=============================================

the idea was that when the forms opened 'littlefixer' is set to = 0,
every time a button is pressed littlefixer grows by '1'. by checking to
see if little fixer was <> 1 i am checking to see if the button has
been pressed (taken the focus) more than once.

i've had to use the focus instead of 'on click' because radio buttons
don't appear to have an 'onclick property'.

i'm getting the following error message though when the got focus code
is exectued.

Compile error: variable not defined.
littlefixer = littlefixer + 1

and it highlights the second 'littlefixer' in the line above.

but i have allready dimm'ed this variable in the onopen of the form so
im stuck.

please help
 
S

SusanV

The variable and it's value don't get carried from one sub to another the
way you might expect, which is why you're getting the "variable not defined"
error. Drop that code out of your OnOpen event, as it isn't doing anything
useful.

Try this code instead on the button's OnClick:

If Me![User Type].Value = "2" Then
Me.[BUSINESS_DETAILS].Visible = True
Else
Me.[BUSINESS_DETAILS].Visible = False
End if



--
hth,
SusanV



Gary said:
I have a strange problem.
I have an option group which consists of two radio buttons.

the option group is tied into a field in my table. and depending on
which button has been pressed the value of the field is either '1' or
'2'.

I want to hide a subform on this form if the value of the field that
the buttons are linked to is '2'. However if the value of the field is
'1' i want to show the subform. I have some code set to run when either
button gets focus. This code checks the value of the field and hides /
unhides the subform accordingly (the code is just below a bit in this
message.)

I have a snag....

It's doing something strange however. the first time either button is
pressed it works fine doing what it should do, however the second and
subsequent times the button is pressed it behaves erraticly and
unreliably. So my solution is to only run the code if it's the first
time the button has been pressed. I thought i'd accomplish this by
putting a bit of code before the code I allready had, as follows.

=================================================================
I put the following code in the 'on open' part of the form.

dim littlefixer as integer
littlefixer = 0
------------------------------------------------------------------

then ... i modified the buttons event code as follows

' added code
littlefixer = littlefixer + 1
If littlefixer <> "1" Then End
Else

'my original code

If Me![User Type].Value = "2" Then Me.[BUSINESS_DETAILS].Visible = True
If Me![User Type].Value = "1" Then Me.[BUSINESS_DETAILS].Visible =
False
End Sub

=============================================

the idea was that when the forms opened 'littlefixer' is set to = 0,
every time a button is pressed littlefixer grows by '1'. by checking to
see if little fixer was <> 1 i am checking to see if the button has
been pressed (taken the focus) more than once.

i've had to use the focus instead of 'on click' because radio buttons
don't appear to have an 'onclick property'.

i'm getting the following error message though when the got focus code
is exectued.

Compile error: variable not defined.
littlefixer = littlefixer + 1

and it highlights the second 'littlefixer' in the line above.

but i have allready dimm'ed this variable in the onopen of the form so
im stuck.

please help
 
D

Dirk Goldgar

Gary said:
I have a strange problem.
I have an option group which consists of two radio buttons.

the option group is tied into a field in my table. and depending on
which button has been pressed the value of the field is either '1' or
'2'.

I want to hide a subform on this form if the value of the field that
the buttons are linked to is '2'. However if the value of the field is
'1' i want to show the subform. I have some code set to run when
either button gets focus. This code checks the value of the field and
hides / unhides the subform accordingly (the code is just below a bit
in this message.)

I have a snag....

It's doing something strange however. the first time either button is
pressed it works fine doing what it should do, however the second and
subsequent times the button is pressed it behaves erraticly and
unreliably. So my solution is to only run the code if it's the first
time the button has been pressed. I thought i'd accomplish this by
putting a bit of code before the code I allready had, as follows.

=================================================================
I put the following code in the 'on open' part of the form.

dim littlefixer as integer
littlefixer = 0
------------------------------------------------------------------

then ... i modified the buttons event code as follows

' added code
littlefixer = littlefixer + 1
If littlefixer <> "1" Then End
Else

'my original code

If Me![User Type].Value = "2" Then Me.[BUSINESS_DETAILS].Visible =
True If Me![User Type].Value = "1" Then Me.[BUSINESS_DETAILS].Visible
= False
End Sub

=============================================

the idea was that when the forms opened 'littlefixer' is set to = 0,
every time a button is pressed littlefixer grows by '1'. by checking
to see if little fixer was <> 1 i am checking to see if the button has
been pressed (taken the focus) more than once.

i've had to use the focus instead of 'on click' because radio buttons
don't appear to have an 'onclick property'.

i'm getting the following error message though when the got focus code
is exectued.

Compile error: variable not defined.
littlefixer = littlefixer + 1

and it highlights the second 'littlefixer' in the line above.

but i have allready dimm'ed this variable in the onopen of the form so
im stuck.

please help

Your immediate problem is that declaring the variable in the Open event
makes it local to that event procedure alone -- you'd have to declare it
at module level, outside of any procedure, for it to be available to
multiple procedures in the module.

However, I believe you are going about this the wrong way. You should
not (IMO) be trying to do this in the GotFocus event, nor for that
matter in any event of either of the option buttons themselves. If the
buttons are really part of an option group, then it's the AfterUpdate
event of the *option group* that should run the code -- that and the
form's Current event. So, if the option group is named "UserType", you
should have the following event procedures:

'----- start of code -----
Private Sub UserType_AfterUpdate()

Me.[BUSINESS_DETAILS].Visible = (Me.UserType = 2)

End Sub

Private Sub Form_Current()

Me.[BUSINESS_DETAILS].Visible = (Me.UserType = 2)

End Sub
'----- end of code -----

That ought to do it.
 
A

Al Camp

Gary,
You'll need two codings.
First, using the AfterUpdate event of your option group...
If MyOpt = 1 Then
Me.[BUSINESS_DETAILS].Visible = False
Else
Me.[BUSINESS_DETAILS].Visible = True
End Sub

Then use that same code in the OnCurrent event of the form so that, as
you browse from record to record, the subform will hide/show accordingly.
If your form still "behaves erratically" then you have some other
problem.
--
hth
Al Camp
Candia Computer Consulting - Candia NH
http://home.comcast.net/~cccsolutions


Gary said:
I have a strange problem.
I have an option group which consists of two radio buttons.

the option group is tied into a field in my table. and depending on
which button has been pressed the value of the field is either '1' or
'2'.

I want to hide a subform on this form if the value of the field that
the buttons are linked to is '2'. However if the value of the field is
'1' i want to show the subform. I have some code set to run when either
button gets focus. This code checks the value of the field and hides /
unhides the subform accordingly (the code is just below a bit in this
message.)

I have a snag....

It's doing something strange however. the first time either button is
pressed it works fine doing what it should do, however the second and
subsequent times the button is pressed it behaves erraticly and
unreliably. So my solution is to only run the code if it's the first
time the button has been pressed. I thought i'd accomplish this by
putting a bit of code before the code I allready had, as follows.

=================================================================
I put the following code in the 'on open' part of the form.

dim littlefixer as integer
littlefixer = 0
------------------------------------------------------------------

then ... i modified the buttons event code as follows

' added code
littlefixer = littlefixer + 1
If littlefixer <> "1" Then End
Else

'my original code

If Me![User Type].Value = "2" Then Me.[BUSINESS_DETAILS].Visible = True
If Me![User Type].Value = "1" Then Me.[BUSINESS_DETAILS].Visible =
False
End Sub

=============================================

the idea was that when the forms opened 'littlefixer' is set to = 0,
every time a button is pressed littlefixer grows by '1'. by checking to
see if little fixer was <> 1 i am checking to see if the button has
been pressed (taken the focus) more than once.

i've had to use the focus instead of 'on click' because radio buttons
don't appear to have an 'onclick property'.

i'm getting the following error message though when the got focus code
is exectued.

Compile error: variable not defined.
littlefixer = littlefixer + 1

and it highlights the second 'littlefixer' in the line above.

but i have allready dimm'ed this variable in the onopen of the form so
im stuck.

please help
 
S

SusanV

Sweet - one day I'm going to get the hang of these one-line if/else settings

;-)


Dirk Goldgar said:
Gary said:
I have a strange problem.
I have an option group which consists of two radio buttons.

the option group is tied into a field in my table. and depending on
which button has been pressed the value of the field is either '1' or
'2'.

I want to hide a subform on this form if the value of the field that
the buttons are linked to is '2'. However if the value of the field is
'1' i want to show the subform. I have some code set to run when
either button gets focus. This code checks the value of the field and
hides / unhides the subform accordingly (the code is just below a bit
in this message.)

I have a snag....

It's doing something strange however. the first time either button is
pressed it works fine doing what it should do, however the second and
subsequent times the button is pressed it behaves erraticly and
unreliably. So my solution is to only run the code if it's the first
time the button has been pressed. I thought i'd accomplish this by
putting a bit of code before the code I allready had, as follows.

=================================================================
I put the following code in the 'on open' part of the form.

dim littlefixer as integer
littlefixer = 0
------------------------------------------------------------------

then ... i modified the buttons event code as follows

' added code
littlefixer = littlefixer + 1
If littlefixer <> "1" Then End
Else

'my original code

If Me![User Type].Value = "2" Then Me.[BUSINESS_DETAILS].Visible =
True If Me![User Type].Value = "1" Then Me.[BUSINESS_DETAILS].Visible
= False
End Sub

=============================================

the idea was that when the forms opened 'littlefixer' is set to = 0,
every time a button is pressed littlefixer grows by '1'. by checking
to see if little fixer was <> 1 i am checking to see if the button has
been pressed (taken the focus) more than once.

i've had to use the focus instead of 'on click' because radio buttons
don't appear to have an 'onclick property'.

i'm getting the following error message though when the got focus code
is exectued.

Compile error: variable not defined.
littlefixer = littlefixer + 1

and it highlights the second 'littlefixer' in the line above.

but i have allready dimm'ed this variable in the onopen of the form so
im stuck.

please help

Your immediate problem is that declaring the variable in the Open event
makes it local to that event procedure alone -- you'd have to declare it
at module level, outside of any procedure, for it to be available to
multiple procedures in the module.

However, I believe you are going about this the wrong way. You should
not (IMO) be trying to do this in the GotFocus event, nor for that
matter in any event of either of the option buttons themselves. If the
buttons are really part of an option group, then it's the AfterUpdate
event of the *option group* that should run the code -- that and the
form's Current event. So, if the option group is named "UserType", you
should have the following event procedures:

'----- start of code -----
Private Sub UserType_AfterUpdate()

Me.[BUSINESS_DETAILS].Visible = (Me.UserType = 2)

End Sub

Private Sub Form_Current()

Me.[BUSINESS_DETAILS].Visible = (Me.UserType = 2)

End Sub
'----- end of code -----

That ought to do it.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
J

jahoobob

Maybe the code here: http://www.mvps.org/access/forms/frm0029.htm ca
help. It hides the subform if no records are present. Perhaps you ca
modify it to suit your needs.
Gary said:
I have a strange problem.
I have an option group which consists of two radio buttons.

the option group is tied into a field in my table. and depending on
which button has been pressed the value of the field is either '1' or
'2'.

I want to hide a subform on this form if the value of the field that
the buttons are linked to is '2'. However if the value of the field is
'1' i want to show the subform. I have some code set to run whe
either
button gets focus. This code checks the value of the field and hides /
unhides the subform accordingly (the code is just below a bit in this
message.)

I have a snag....

It's doing something strange however. the first time either button is
pressed it works fine doing what it should do, however the second and
subsequent times the button is pressed it behaves erraticly and
unreliably. So my solution is to only run the code if it's the first
time the button has been pressed. I thought i'd accomplish this by
putting a bit of code before the code I allready had, as follows.

=================================================================
I put the following code in the 'on open' part of the form.

dim littlefixer as integer
littlefixer = 0
------------------------------------------------------------------

then ... i modified the buttons event code as follows

' added code
littlefixer = littlefixer + 1
If littlefixer "1" Then End
Else

'my original code

If Me![User Type].Value = "2" Then Me.[BUSINESS_DETAILS].Visible
True
If Me![User Type].Value = "1" Then Me.[BUSINESS_DETAILS].Visible =
False
End Sub

=============================================

the idea was that when the forms opened 'littlefixer' is set to = 0,
every time a button is pressed littlefixer grows by '1'. by checkin
to
see if little fixer was 1 i am checking to see if the button has
been pressed (taken the focus) more than once.

i've had to use the focus instead of 'on click' because radio buttons
don't appear to have an 'onclick property'.

i'm getting the following error message though when the got focus code
is exectued.

Compile error: variable not defined.
littlefixer = littlefixer + 1

and it highlights the second 'littlefixer' in the line above.

but i have allready dimm'ed this variable in the onopen of the form so
im stuck.

please hel
 
G

Gary

Thank you everyone for your efforts, I tried Dirk's method first
because it was the most succinct and it worked first time =)

what a nice way to start wednesday morning.

thankyou Dirk !

Gary.
 
Top