Clearing about 20 fields in a form

helloworld

Registered User.
Local time
Yesterday, 16:44
Joined
May 18, 2004
Messages
62
Hi there...just a quick question...I have about 20 fields in this form that I need cleared on the push of a button. Is there an easier way to doing this than to go

Me.text1 = null
Me.text2= Null
.
.
.
.
.
.
etc....

Is there a Vb command that selects all the fields in the form so that I could just go

Allfields = null?

Your help would be much apprecitated!
 
If you want to clear everything:
Code:
  Dim MyForm As Form
  Dim ctl As Control
  Set MyForm = Screen.ActiveForm
  For Each ctl In MyForm.Controls 
     ctl = Null
  Next ctl
 
Thanks a lot for your quick reply! I added an undo button which did pretty much the same thing. However, which one would you recommend? Also, is there anywhere for Access to grab the username of the person using the computer at that time and display it in a textbox?
 
Two easy API calls can provide you with network log in name of the current user of the workstation and the network name of the computer itself for use in your Access application. Declare the following functions in your module and add the following function:
Code:
Private Declare Function api_GetUserName Lib "advapi32.dll" Alias _
          "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    
Private Declare Function api_GetComputerName Lib "Kernel32" Alias _
          "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
'================================
Public Function CNames(UserOrComputer As Byte) As String

    'UserorComputer; 1=User, anything else = computer

    Dim NBuffer As String
    Dim Buffsize As Long
    Dim wOK As Long

    Buffsize = 256
    NBuffer = Space$(Buffsize)


    If UOrC = 1 Then
        wOK = api_GetUserName(NBuffer, Buffsize)
         CNames = Trim$(NBuffer)
    Else
        Wok = api_GetComputerName(NBuffer, Buffsize)
        CNames = Trim$(NBuffer)
    End If
End Function

Hope this helps, Mitch :)
 
Hey Mitch...thanks for your reply, but I am a newbie and I am not sure how I would call this function.

Right now I got:

Forms!FormName!TextBox=CNames()

I get an argument error...can you help me?
 
helloworld said:
I added an undo button which did pretty much the same thing.

Code:
Me.Undo

will clear the form if the record is new or will undo the changes if the record is not new.

To set the controls to Null, you may find problems with pbaldy's code if you have labels, boxes, etc. on your form as these can't be set to Null and are included in the form's Controls collection.

Assuming it is just textboxes:

Code:
Dim ctl As Control
For Each ctl In Me
    If ctl.ControlType = acTextBox Then ctl = Null
Next
Set ctl = Nothing

is there anywhere for Access to grab the username of the person using the computer at that time and display it in a textbox?

In the textbox's ControlSource, all you need to put is

Code:
=Environ("username")
 
Mile-O-Phile said:
In the textbox's ControlSource, all you need to put is

Code:
=Environ("username")

Hi Milo,

I tried that and I get this:

#Name?

what does that mean?
 
What version of Access do you have?
 
Maybe you are missing a reference then. Open a module, goto Tools -> References and see if any are marked as Missing. If so, ensure they are checked.
 
helloworld said:
Hey Mitch...thanks for your reply, but I am a newbie and I am not sure how I would call this function.

Right now I got:

Forms!FormName!TextBox=CNames()

I get an argument error...can you help me?

You need to put something in the CNames() like a 1 or a 2.

'UserorComputer; 1=User, anything else = computer

so try this:
Code:
Forms!FormName!TextBox=CNames(1)

Mitch
 
Hey guys...thanks a lot for your help, but I still get this message in the textbox:

#Name?

I might be missing a reference but I am not sure which one...I have over 30 references that are unchecked so I am not sure which one I would need. Can anyone help?
 
The missing reference typically has MISSING written next to it. Open this attachment and tell me what happens when you open it. :)
 

Attachments

thanks a lot Milo and Mitch...I got it working thanks to you guys...but I do have one more question :p

Can you include a combo-box in an input box?
 
I don't think so, but what I do generally is just make a little unbound (to data) form and put my combobox on that then on a button run whatever code, form, report that I need.
 
Once again, thanks for your help guys! I really appreciate it!
 

Users who are viewing this thread

Back
Top Bottom