Advertisement

05.09.2008 at 05:50AM PDT, ID: 23389073 | Points: 250
[x]
Attachment Details
Using same codes in multiple forms
Hi Experts, I am sure this is an easy one. What is the best way to handle using the codes on one form in another. I figure doing this, will reduce the size of the database and also speed up the processing times.
I have some validation rules on the form level, that applies to several other 1 or 2 other forms. I thought i code put the code in a module, but not sure how that would work. Below are some of the validation rules that I have setup on my main form. Can some give me an example how I can set this up. Thanks in advance.

Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err

'Forces user to enter FAT if entry in closing date
If IsNull(Me.cboFat) And Not IsNull(Me.ClosingDate) Then
    MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!"
    Cancel = True
    cboFat.SetFocus
   End If

'Forces user to enter Closing Date if entry in FAT
If Not IsDate(ClosingDate) Then

    Select Case Me.cboFat
       Case 1, 2, 3, 4, 5, 7
            MsgBox "Based on your selection in the Final Action Taken," & _
            vbCrLf & "           Closing Date is a required field!", vbCritical, "Missing Data!"
            Cancel = True
    End Select
End If

' Forces user to enter Actual Amount if FAT = 1
If Me.cboFat = 1 And IsNull(Me.ActualAmount) Then
    MsgBox "Based on your selection in the Final Action Taken," & _
    vbCrLf & "          Actual Amount is a required field!", vbCritical, "Missing Data!"
    Cancel = True
    ActualAmount.SetFocus
End If

If IsNull(Me.cboLTV) Then

    Select Case Me.cmbMtgType
        Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr"
             MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!"
             Cancel = True
             cboLTV.SetFocus
    End Select
End If

Form_BeforeUpdate_Exit:
    Exit Sub
   
Form_BeforeUpdate_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume Form_BeforeUpdate_Exit
   
End Sub
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: nkengie81
Question Asked On: 05.09.2008
Participating Experts: 3
Points: 250
Views: 0
Translate:
Loading Advertisement...
05.09.2008 at 05:54AM PDT, ID: 21532246

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 06:03AM PDT, ID: 21532325

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 06:04AM PDT, ID: 21532334

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.09.2008 at 06:08AM PDT, ID: 21532359

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.12.2008 at 04:58AM PDT, ID: 21546082

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.09.2008 at 05:54AM PDT, ID: 21532246
Hello nkengie81,

Put everything in a public Sub.When you want to validate your entries, use validationform()

Regards,

jppinto
1:
2:
3:
4:
5:
public Sub validationform() 
   Your code here! 
End Sub
Open in New Window
 
05.09.2008 at 06:03AM PDT, ID: 21532325
Ok, on the main form I would say:

public Sub validationform()
  'Forces user to enter FAT if entry in closing date
If IsNull(Me.cboFat) And Not IsNull(Me.ClosingDate) Then
    MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!"
    Cancel = True
    cboFat.SetFocus
   End If

'Forces user to enter Closing Date if entry in FAT
If Not IsDate(ClosingDate) Then

    Select Case Me.cboFat
       Case 1, 2, 3, 4, 5, 7
            MsgBox "Based on your selection in the Final Action Taken," & _
            vbCrLf & "           Closing Date is a required field!", vbCritical, "Missing Data!"
            Cancel = True
    End Select
End If

' Forces user to enter Actual Amount if FAT = 1
If Me.cboFat = 1 And IsNull(Me.ActualAmount) Then
    MsgBox "Based on your selection in the Final Action Taken," & _
    vbCrLf & "          Actual Amount is a required field!", vbCritical, "Missing Data!"
    Cancel = True
    ActualAmount.SetFocus
End If

If IsNull(Me.cboLTV) Then

    Select Case Me.cmbMtgType
        Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr"
             MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!"
             Cancel = True
             cboLTV.SetFocus
    End Select
End If
End Sub

But how do I reference that code in the other forms? Sorry really new to this stuff.
 
05.09.2008 at 06:04AM PDT, ID: 21532334

Rank: Genius

Hello nkengie81,

A bit of elaboration on jppinto's suggestion...

1) Put that sub in a regular VBA module, not in a form or report module

2) You may need to pass the name of the form as an argument.  So your "public sub may look like:

Sub Validate(NameOfForm As String)

    'some code here

End Sub

and then you call it like this from a Form event:

Private Sub cmdGo_Click()

    Validate "NameOfFormGoesHere"

End Sub


Passing the form name as a parameter will facilitate being able to reference specific controls
and properties of the form you need to validate/manipulate.

Regards,

Patrick
 
05.09.2008 at 06:08AM PDT, ID: 21532359
you can create a separate module for these subs and functions. As long as you set them to be public you'll be able to access them from any form in the usual manner.

Although you'll probably want to modify your subs to be functions that you can pass variables into for where the code is refering to controls.


'Keep this in the forms.
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Form_BeforeUpdate_Err

Dim result as string

result = ValidateForm(me.cboFat,me.ClosingDate,me.cboLTV,me.ActualAmount,me.cmbMtgType)

if result <> 'Success' then
    Cancel = True
    me.controls(result).setfocus
end if

Form_BeforeUpdate_Exit:
    Exit Sub
   
Form_BeforeUpdate_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume Form_BeforeUpdate_Exit
   
End Sub


'Put this in a separate module

Public Function ValidateForm(Fat,ClosingDate,LTV,ActualAmount,MtgType) as string
'Forces user to enter FAT if entry in closing date
If IsNull(Fat) And Not IsNull(ClosingDate) Then
    MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!"
    ValidateForm = "cboFat"
    exit function
   End If

'Forces user to enter Closing Date if entry in FAT
If Not IsDate(ClosingDate) Then

    Select Case Fat
       Case 1, 2, 3, 4, 5, 7
            MsgBox "Based on your selection in the Final Action Taken," & _
            vbCrLf & "           Closing Date is a required field!", vbCritical, "Missing Data!"
            ValidateForm = "ClosingDate"
            exit function
    End Select
End If

' Forces user to enter Actual Amount if FAT = 1
If Fat = 1 And IsNull(ActualAmount) Then
    MsgBox "Based on your selection in the Final Action Taken," & _
    vbCrLf & "          Actual Amount is a required field!", vbCritical, "Missing Data!"
    ValidateForm = "ActualAmount"
    exit function
End If

If IsNull(LTV) Then

    Select Case MtgType
        Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr"
             MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!"
             ValidateForm = "cboLTV"
             exit function
    End Select
End If

ValidateForm = "Success"

End function
 
05.12.2008 at 04:58AM PDT, ID: 21546082
I think I ran into a problem. The post above I am sure will solve my problems, but I am not sure how to handle using the code below without complicating the codes. This is what I have before I posted this question. I am trying to use this same codes on another form. If I put it in a module I am not sure how it will affect the current form that holds that code. Please advise.  Thanks

Private Function FormDoesntValidate() As Boolean
    On Error GoTo FormDoesntValidate_Err
    FormDoesntValidate = False
   
    'Forces user to enter FAT if entry in closing date
    If IsNull(Me.cboFat) And Not IsNull(Me.ClosingDate) Then
        MsgBox "Final Action Taken is required if a Closing Date is entered,", vbCritical, "Missing Data!"
        FormDoesntValidate = True
        'Me.cboFat.SetFocus
   End If
   
    ' Forces user to enter Actual Amount if FAT = 1
    If Me.cboFat = 1 And IsNull(Me.ActualAmount) Then
        MsgBox "Based on your selection in the Final Action Taken," & _
        vbCrLf & "          Actual Amount is a required field!", vbCritical, "Missing Data!"
        FormDoesntValidate = True
        'ActualAmount.SetFocus
    End If
 
    'Forces user to enter Closing Date if entry in FAT
    If Not IsDate(Me.ClosingDate) Then
        Select Case Me.cboFat
            Case 1, 2, 3, 4, 5, 7
                MsgBox "Based on your selection in the Final Action Taken," & _
                vbCrLf & "          Closing Date is a required field!", vbCritical, "Missing Data!"
                FormDoesntValidate = True
        End Select
    End If
   
    If IsNull(Me.cboLTV) Then
        Select Case Me.cmbMtgType
            Case "2 Yr ARM", "15 Yr", "20 Yr", "25 Yr", "30 Yr"
                MsgBox "The selected Mortgage Type requires a LTV selection.", vbCritical, "Missing Data!"
                FormDoesntValidate = True
                'cboLTV.SetFocus
        End Select
    End If
 
FormDoesntValidate_Exit:
    Exit Function
   
FormDoesntValidate_Err:
    MsgBox Err.Number & " - " & Err.Description
    Resume FormDoesntValidate_Exit
End Function
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628