Reduce Code

  • Thread starter Thread starter Garry Jones
  • Start date Start date
G

Garry Jones

I am hiding or showing a textbox on a form depending on whether or
not it is empty. As I want to do this for more textboxes I would like
reduce the coding for this proceedure.

I can't quite grasp the syntax and would be gratefull for any help.

Ideally I would just run the same proceedure on a number of textboxes
and send each textbox to the same proceedure to show or hide.

This is what I am doing

Private Sub Form_Current()
If IsNull([Dat1]) Then
hdbx1
Else
shwbx1
End If
If IsNull([Dat2]) Then
hdbx2
Else
shwbx2
End If
End Sub

Private Sub shwbx1()
Dat1.Visible = True
End Sub

Private Sub hdbx1()
Dat1.Visible = False
End Sub

Private Sub shwbx2()
Dat2.Visible = True
End Sub

Private Sub hdbx2()
Dat2.Visible = False
End Sub

Very longwinded and as I have a lot more textboxes I really want to make
this code more concise. This is what I have in mind...

Private Sub Form_Current()
chk[dat1],[dat2], etc
(i want this to send each textbox in turn to chk, can't grasp syntax)
End Sub

Private Sub chk()
(I want to use the "IsNull" and then send each textbox on to either one
of the following proceedures according to result of "IsNull", can't
grasp syntax)
End Sub

Private Sub shwbx()
"actual textbox".visible = true
End Sub

Private Sub hdbx()
"actual textbox".visible = false
End Sub


Garry Jones
Sweden
 
Garry Jones said:
I am hiding or showing a textbox on a form depending on whether or
not it is empty. As I want to do this for more textboxes I would like
reduce the coding for this proceedure.

I can't quite grasp the syntax and would be gratefull for any help.

Ideally I would just run the same proceedure on a number of textboxes
and send each textbox to the same proceedure to show or hide.
Call a function from your form and pass the form object to it (untested
aircode):

Call MyFunction(Me) 'Put this in your form's Current event

In your function:

Public Function MyFunction(frmForm As Form)

Dim ctl As Control
For Each ctl In frmForm
If IsNull(ctl.Value) Then
ctl.Visible = False
Next

End Function

HTH - Keith.
www.keithwilby.com
 
Garry

From a user/usability point of view, I find it disquieting to have fields on
the screen appearing and disappearing.

From a programming point of view, I'll have to assume you will only use this
form to display data that's been entered elsewhere. Otherwise, all (empty)
controls will be invisible, and the user cannot enter data into them!

JOPO (just one person's opinion)

Jeff Boyce
<Office/Access MVP>
 
Jeff said:
From a user/usability point of view, I find it disquieting to have fields on
the screen appearing and disappearing.

I understand. The idea of this is to enter data during January and
February that will then be viewed during the rest of the year. Maybe
"forms" is not the best function to view data, but I find the colour and
layout functions to be sufficient for my needs.
 
Garry

Now I'm confused... (not a new state <g>)

Are you saying that you have controls on your form for entering "January
{something}" and "February {something}"?

Does this mean your form has a control for each month? Does this mean your
underlying table structure has a field dedicated to each month?

Curious minds want to know...

Regards

Jeff Boyce
<Office/Access MVP>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top