calling global functions vs. writing repetition behind forms

neuroman9999

Member
Local time
Today, 08:00
Joined
Aug 17, 2020
Messages
798
can any of you experts tell me if makes any sense whatsoever? I have a scenario where security paranoia is running rampant in a large American bank, who I am working for. I know what to do, obviously, but there are endless ways to tackle stuff like this. Here is what I am currently doing:

when a new main form record is entered, it has to be assigned a status, however, that status cannot be assigned and put into the binding until a sign off form is filled out and a UN and hashed PW is checked. so, here's a sample of code, that is not that efficient, behind the main form:
PHP:
Private Sub btnSaveRec_Click()
If Me.NewRecord Then
    If Me.Dirty Then
        If validate_Controls_Private("status", "statusnotification") Then
            Me.btnApprove.Enabled = True
            Me.Status = "entered"
            Me.statusnotification = "Entered, Awaiting Approval"
            DoCmd.RunCommand acCmdSaveRecord
            DoCmd.OpenForm "frmSignOff"
        Else
            MsgBox "All fields must be filled in for new records.", vbCritical, "Error"
        End If
    Else
        MsgBox "No data has been entered for this record.", vbCritical, "Error"
    End If
Else 'record NOT NEW
    If Me.Dirty Then
        If Me.Status = "approved" Then 'record already locked
            MsgBox "This record cannot be modified or saved because " & vbCrLf & _
                   "it has already been approved and signed.  It is " & vbCrLf & _
                   "locked permanently.", vbCritical, "Error"
            Me.Undo
        Else
            DoCmd.RunCommand (acCmdSaveRecord)
            MsgBox "Record has been saved.", vbInformation, "Confirm"
        End If
    Else
        MsgBox "No changes have been made to the record." & vbCrLf & _
               "Record cannot be saved.", vbCritical, "Error"
    End If
End If
End Sub
and even though that code is a tad bit wrong, there are other parts of the app that make perfect sense. for instance, in another area, there is this code:
PHP:
Private Sub btnClose_Click()
If Me.NewRecord Then 'IS new record.
    If Me.Dirty Then 'new record entry has been started.
        If validate_Controls_Private("status", "statusnotification") Then 'if all fields filled in.
            If MsgBox("You have not saved the current record." & vbCrLf & _
                      "Would you like to do so before closing the form?", vbYesNo + vbExclamation, "Confirm") = vbYes Then
                MsgBox "Saving new records requires you to sign off on the process." & vbCrLf & _
                       "You will now be redirected to the Sign Off form....", vbExclamation, "Alert"
                DoCmd.OpenForm "frmSignOff"
            Else
                Me.Undo
                DoCmd.Close acForm, Me.Name
            End If
        Else 'some fields are NOT filled in, ask if user wants to abondon efforts.
            If MsgBox("All fields must be filled in for new records." & vbCrLf & _
                      "Would you like to cancel this record entry and close the form?", vbYesNo + vbExclamation, "Confirm") = vbYes Then
                Me.Undo
                DoCmd.Close acForm, Me.Name
            End If
        End If
    Else
        DoCmd.Close acForm, Me.Name
    End If
Else 'NOT a new record.
    If Me.Dirty Then 'OLD records can ONLY be edited if ''status'' = ''entered''.  record HAS been edited.
        If MsgBox("You have made changes to the current record." & vbCrLf & _
                  "Would you like to save your changes before closing the form?", vbYesNo + vbExclamation, "Confirm") = vbYes Then
            DoCmd.RunCommand acCmdSaveRecord
            MsgBox "Record has been saved.", vbInformation, "Confirm"
            DoCmd.Close acForm, Me.Name
        Else
            Me.Undo
            DoCmd.Close acForm, Me.Name
        End If
    Else 'record NOT edited.
        DoCmd.Close acForm, Me.Name
    End If
End If
End Sub
and the called function in the form's module is:
PHP:
Private Function validate_Controls_Private(exclude As String, exclude2 As String) As Boolean
For Each c In Me.controls
    If TypeOf c Is TextBox And (c.Name <> exclude And c.Name <> exclude2) Then
        If (c = "" Or IsNull(c)) Then
            validate_Controls_Private = False
            Exit Function
        End If
    End If
 Next c
    validate_Controls_Private = True
End Function
can anyone see how this can be reduced? it's not a big deal, but none-the-less, there is still more code written than need be, obviously. thanks.
 
Also, calling it only for new records means that once a record has been saved once, you code allows required fields to be reverted to null.
that has been covered elsewhere in the code. it's not perfect code Pat, and that's because I'm not an access pro. but none-the-less, it does the job.

I will use your wisdom next time. the code is too long to go back now. and I was unaware of the ""official"" way to validate anything.
 
Holy cow bad that's a lot to take in period let me read it later and get back to you if I got any questions about it thanks
 
The most important thing about becoming proficient in Access is an understanding of how to correctly use various Form and Control level events to control the validation of data and ensuring that your forms only allow valid data to be saved..

So, yes, there really is an "official" way to do things in Access.
but business rarely goes that way, because so many people are literally stupid and understand nothing. :( and I do not have the time to spend learning everything about this program because I write code in a lot of languages and offer guidance to many others. and everyone uses different tools.

by the way, speaking of different tools and using Satan against himself, did you know that right now you can take out literally and endless of amount of credit cards and get cash out of each one of them if they have the visa/mastercard/any bank name logo on them? I have done this, and have gotten 4 cards in the last 3 weeks strictly for the purpose of purchasing products at various places around my area of the world and paying off each card to 0 at the end of each month. I am doing it for 2 reasons:

=> 1) to trick corporate software into believing that I'm rich and can manage money on many different lines.
=> 2) giving myself a huge amount of financial backup reserves just in case the idiots that run the world can't get a handle on themselves, the virus carries on forever and my business takes a hit (although I have plenty of connections with DHS to get food for free anyway), or a civil war starts because people can't seem to let go of the George Floyd incident.
 
I am not going to say what I think about this statement. We'll just keep cleaning up the messes you make with your bad advice.
likewise. i won't even consider you either. I do a lot of good, and you will never see it. if you don't like it, quit engaging. it's that simple. DUH
 

Users who are viewing this thread

Back
Top Bottom