views:

46

answers:

3

Hi,

Would anybody know how to determine what a forms data entry mode is from within VB.

The reason for doing this is to be able to hide/show controls depending on whether or not the user is adding or editing records eg:

if Me.DataEntry = New then
Me.comboBoxForEdits.Visible = false
end if

Thanks in advance for any help

Cheers

Noel

+2  A: 

How about

If Me.NewRecord Then
   Me.SomeControl.Visible=false
End If
Remou
Thats great Remou cheers. I was trying to use "If Me.NewRecord = True Then ..." Rookie mistake!
glinch
I'm not sure why what you say you were using wouldn't have worked, since Me.NewRecord returns a Boolean and in VB/VBA True is evaluated as Boolean True.
David-W-Fenton
Hi David, Have tried the code with "Me.NewRecord = True" and yes it most definitely works. Slightly confused as to what I was using earlier that never worked. Nevertheless, thanks for clarifying the issue. Cheers Noel
glinch
+1  A: 
Private Sub Form_Current()

    If Me.AllowAdditions = True Then
        ' set controls as required.
    End If    
End Sub

Adding that code to the forms Current event should work.

matt
You can Allow Additions without the user being in the act of adding a record.
Jeff O
+1  A: 

Another way to write this code could be:

  Me!SomeControl.Visible = Not Me.NewRecord

It would be somewhat different from @Remou's code. This would be the equivalent of the above:

  If Me.NewRecord Then
     Me!SomeControl.Visible = False
  Else
     Me!SomeControl.Visible = True
  End If

Now, it could very well be that you don't want to change the control's .Visible property if it's not a new record, so @Remou's original suggestion may be correct.

Another suggestion is if you are performing this operation on large numbers of controls, you might want to create a custom collection in the form's OnLoad event and have the collection hold pointers to controls you want to operate on. I do this all the time when I need to change the values/appearance/visibility of large numbers of controls in the OnCurrent event.

David-W-Fenton
Cheers David great advice, I appreciate the help. Will take a look at creating a custom collection, this could be a very useful technique.Thanks Noel
glinch
In regard to custom collections, have a look at this post of mine: http://stackoverflow.com/questions/1917981/how-to-use-controls-collection-in-access-2003-and-vba/1932103#1932103
David-W-Fenton