command button to disappear and reappear

AOGFADUGBA

Board Regular
Joined
Sep 30, 2015
Messages
74
Hi all i want a command only to be active when only when on a new record.
Below code runs when the form opens
Code:
Private Sub Form_Load()
Me.Command31.Visible = False
End Sub

Code:
Private Sub Command43_Click()
DoCmd.GoToRecord , , acNewRec
Me.Command31.Visible = True
End Sub

The code runs fine but i want the button to disappear if the user navigates off the new record.
Can anyone help?
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I don't see how that link will help because it's for command buttons on a spreadsheet and spreadsheets don't have 'new records'. Try testing the form's NewRecord property in the Current event (note; air code, not tested):

Code:
Private Sub myForm_Current()
If Me.NewRecord Then
   Me.Command31.Visible = False
Else
   Me.Command31.Visible = True
End If
I am not super familiar with ALL form events, so lookup form events to see if there is a more suitable one if this doesn't quite work.

Whether or not you use the Else portion while setting control properties is debatable. It is possible to alter a property, hit a break in code, switch to design view and make some other change, then save the form. Your code-set property will be saved too for when you next open the form. The Else portion would negate that possibility.

P.S. when you have to review lots of code, names like Command31 provide no clue and are pretty much useless. I'd suggest you look into Access naming conventions and adopt a good practice.
 
Upvote 0
Hi Micron and thanks once again for another great suggestion. i modified the code and it worked fine. see below

Code:
Private Sub cmdNewRecord_Click()
DoCmd.GoToRecord , , acNewRec
Me.Command31.Visible = True
End Sub


Private Sub Form_Current()
If Me.NewRecord Then
   Me.Command31.Visible = True
Else
   Me.Command31.Visible = False
End If
End Sub
 
Upvote 0
Hi Micron, is it also possible for me to add a code to make sure that all fields in the form are filled before the command button becomes active?
The fields are date, code, particulars and amount.
Thanks
 
Upvote 0
Yes, and I can help you with that. First, consider if you'd rather trap for when all fields are not supplied because it's more user friendly. Maybe not now but down the road, some new user will question why the button is visible but inactive. I've found that what users lack in spades is the designer's intuition. Unless you add descriptive labels (which is not the way to go) they won't understand. If you agree, there are two ways to handle this:
1) just code for the four fields you ask for, or
2) I can provide code for a function that loops through all form controls and looks for a tag value. This will be robust because you put the function into a standard module and pass the form name and tag that you want to look for. The function will return a string something like "You must enter data for: and then list each control you have tagged as "required" on your form. The tag property is on the Other tab of the property sheet. AFAIK, every control you can put on a form or report can have a tag, so there should be no limitations.
 
Upvote 0
OK. All you need to do now is let me know which of the two approaches I mentioned is the one you want to take.
 
Upvote 0
The fields are date, code, particulars and amount.
I suggest you research 'reserved words' for Access. You must not (I think CANNOT) use Date for a field name. There is a big list and you should be aware of them. Also, research on object naming conventions and adopt some sort of habit because Command31 is a useless name, but it's what I will have to work with.

This code should work for any form, but it relies on two conditions.
1) You have entered a tag value for each control that you want to enforce an entry by the user. This is done on the property sheet for each control. You can do this for any number of controls all at once by clicking on them while holding down the shift key then entering the tag. I suggest to use text only - dates and numbers may introduce problems. For this code I have used reqd for the tag but you can use whatever you want provided it is not a reserved name. required is a reserved name. If you use something else, you have to change what I have in the function call.
2) The control's label must be attached, otherwise the code will fail when it can't get the member of its collection (Controls(0)). The work around would be to get the control name itself, but not real useful if you allow names like Text15.

One of the best places to put the call to this function is the button click event that attempts to process the user input. If you want to check a form that is loaded but is not the one with the button, replace Me with the proper form reference. So for your event, the code should look like this (in addition to whatever else you need for the event):
Code:
Private Sub Command31_Click()
Dim svMsg As String

svMsg = CheckRequired(Me, "reqd")
If svMsg <> "" Then
    MsgBox svMsg
    Exit Sub
End If

End Sub

To be able to use this function for all forms in your project, it must be in a standard module, not a form or report module.
Code:
Function CheckRequired(frm As Form, svTag As String) As String
Dim ctl As Control
Dim svList As String, svMsg As String

'ensure function and list = empty string to start
CheckRequired = ""
svList = ""
svMsg = "You must provide data for:" & vbCrLf

On Error Resume Next 'if controltype doesn't allow attached label, move to the next control
'A Select Case block could process only certain types; this is more concise & works just as well. 

'iterate through all controls on the form that was passed to the function
For Each ctl In frm
  If ctl.Tag = svTag Then 'look for the tag value passed to the function
    If IsNull(ctl) Or ctl = "" Then svList = svList & " - " & ctl.Controls(0).Caption & vbCrLf
  End If
Next ctl

If svList <> "" Then 'if list not empty, concatenate it to message; set function = message value
  svMsg = svMsg & svList
  CheckRequired = svMsg
End If 'otherwise, function still = ""
End Function

Please ensure you have understood all the instructions to implement this if you have problems. If you have real basic questions (about property sheets or where to see reserved names) please do some research. Don't post back just saying "It doesn't work". Be specific and include any error message number and message text. If need be, ensure you have the database option to break on all errors (or at least unhandled errors) or introduce your own code break in order to know on which line the error occurs. The code I wrote is not 'air code'. It works, and I hope it works for you too. What it does not do is ensure that the user entered data complies with any database requirement, such as not entering text where a number is required. That has to be dealt with in the rest of your code and/or table properties.
Good luck!
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top