|
|
|
Clean up a form |
| message from =?Utf-8?B?SmVycnk=?= on 24 May 2004 |
Is there a method to initialize all controls in a form? That means move blanks to text control or zero to numeric control.
|
| Brian Kastel replied to =?Utf-8?B?SmVycnk=?= on 24 May 2004 |
Perhaps this might help?
I wrote this code for an application at my last job. It is suitable for a
global module, so I called it when a form loaded. My routine examines the
bound controls on a form, setting the color of the attached labels to one
color for required fields, and another color for non-required fields.
Something similar to this is what you will probably have to do to
differentiate between text and numeric fields, but I could be wrong.
=== START OF CODE ==================
|
| =?Utf-8?B?SmVycnk=?= replied to Brian Kastel on 24 May 2004 |
Thansk Brian. When I went through your code, I found that the form is bounded to a recordset. But how about if my form does not have recordsets. I have a blank form just for input. after finished input, my prgram will perform validation and update. After update the recordsets will be closed. In order for the form be available for next input, I want to clear (blank out) all controls in ther form.
Thanks for your code. It is beautiful. Actually you have given me some idea to use color for highlight if that control does not pass validation.
|
| Brian Kastel replied to =?Utf-8?B?SmVycnk=?= on 24 May 2004 |
Thanks for the compliment, Jerry.
If I understood your original question, you want to initialize some controls
to empty strings and others to numeric zeroes, depending upon their intended
future binding. If this is the case, then perhaps you could modify the code
to examine the *intended* record source first to determine the data types.
Then, if your form's text control names are identical to the recordset's
field names, you could enumerate through them that way.
The code would no longer be suitable for a global module, I don't think, but
you could just place the code into the form's module and it wouldn't be an
issue.
I just worked on this, and it seems to work fine:
=== START OF CODE =================
Private Sub InitControls()
Dim ctl As Control
Dim rs As Recordset
Dim fld As Field
Dim DefVal As Variant
'Examine the intended record source.
Set rs = CurrentDb.OpenRecordset("tblTest", dbOpenDynaset)
'Enumerate through the fields.
For Each fld In rs.Fields
'Use defaults specified in the table def.
If Not fld.DefaultValue = "" Then
DefVal = fld.DefaultValue
Else
'Set the default by the field's data type.
Select Case fld.Type
Case dbBoolean
DefVal = False
Case dbByte, dbCurrency, dbDecimal, dbDouble, _
dbFloat, dbInteger, dbLong, dbNumeric, _
dbSingle
DefVal = 0
Case dbDate, dbTime
DefVal = Now
Case dbMemo, dbText
'Deal with the zero-length attribute.
If fld.AllowZeroLength Then
DefVal = ""
Else
DefVal = Null
End If
End Select
End If
'Set the related control's value.
For Each ctl In Me.Controls
If ctl.Name = fld.Name Then
'Do not attempt to set autonumber field.
If (fld.Attributes And dbAutoIncrField) = 0 Then
ctl = DefVal
Exit For
End If
End If
Next ctl
Next fld
'Close the recordset.
rs.Close
'Destroy the object variable.
Set rs = Nothing
End Sub
=== END OF CODE =================
"Jerry" <anonymous@discussions.microsoft.com> wrote in message
news:7678E240-F48A-4B20-A0AE-4C21C8F810EA@microsoft.com...
bounded to a recordset. But how about if my form does not have recordsets.
I have a blank form just for input. after finished input, my prgram will
perform validation and update. After update the recordsets will be closed.
In order for the form be available for next input, I want to clear (blank
out) all controls in ther form.
idea to use color for highlight if that control does not pass validation.
|
| Brian Kastel replied to Brian Kastel on 24 May 2004 |
The Exit For should come after the End If that follows it.
as shown:
'Set the related control's value.
For Each ctl In Me.Controls
If ctl.Name = fld.Name Then
'Do not attempt to set autonumber field.
If (fld.Attributes And dbAutoIncrField) = 0 Then ctl =
DefVal
Exit For
End If
Next ctl
|
| =?Utf-8?B?SmVycnk=?= replied to Brian Kastel on 24 May 2004 |
Thanks very much.
|
|