How to call a module from the form (1 Viewer)

a.sinatra

quik
Local time
Yesterday, 16:22
Joined
Jan 10, 2004
Messages
262
Code:
Public Function fConversion()
    
    'Checks to see if data is entered in first box
    If IsNumeric(Me!cmConversion1) Then
        
        'Convert centemeters to inches
        Dim convert As Double
        Dim cm As Double
        
        convert = Me!cmConversion1
        cm = 0.393700787
        
        Me!cmConversion2 = fWidthRound(Left(cm * convert, 5))
        
    Else
    
        'Displays error and resets everything, and gives focus to first box
        MsgBox "Please specify numbers only, letters and words cannot be converted.", vbExclamation, ErrTitle
        Me!cmConversion1 = ""
        Me!cmConversion2 = ""
        Me!cmConversion1.SetFocus
        
    End If
    
End Function

This code shows up twice in 3 different forms, how do i call it to run, and does the code structure look ok.

Im sorry for asking so many questions, i should be getting access bible 2003 very soon, i got access inside out, but it doesnt help much (its too basic)
________
TOYOTA MOTOR MANUFACTURING DE BAJA CALIFORNIA SPECIFICATIONS
 
Last edited:

spacepro

Registered User.
Local time
Today, 00:22
Joined
Jan 13, 2003
Messages
715
You call this by putting the following in the Onclick box of the properties box or whatever property you want to trigger the code on.

Code:
=fConversion()

Not to sure about the code itself.

Hope this helps

Andy
 

Mile-O

Back once again...
Local time
Today, 00:22
Joined
Dec 10, 2002
Messages
11,316
a.sinatra said:
Im sorry for asking so many questions, i should be getting access bible 2003 very soon, i got access inside out, but it doesnt help much (its too basic)

I've not seen the book in question but based on my memory of other books in the series it won't take you this far into Access.

For VBA the best I've seen so far is the Access Developer's Handbook.
 

a.sinatra

quik
Local time
Yesterday, 16:22
Joined
Jan 10, 2004
Messages
262
spacepro said:
You call this by putting the following in the Onclick box of the properties box or whatever property you want to trigger the code on.

Code:
=fConversion()

Not to sure about the code itself.

Hope this helps

Andy

Couldnt get that to work,

Code:
Call fConverison

works, but i have an error inside my module im assuming
________
Honda accord specifications
 
Last edited:

Mile-O

Back once again...
Local time
Today, 00:22
Joined
Dec 10, 2002
Messages
11,316
Your code is restricted to a single form.

I've changed it so that you can place it in a module.

To call it, put this in the second texbox's ControlSource: =fConversion([Conversion1])

Code:
Public Function fConversion(ByVal varCentimetres As Variant) As Variant

    On Error Goto Err_fConversion

    Const cm = 0.393700787
    
    fConversion = fWidthRound(Left(varCentimetres * cm, 5))
    
Exit_fConversion:
    Exit Function

Err_fConversion:
    fConversion = "-"
    MsgBox Err.Description, vbExclamation, "Error #" & Err.Number
    Resume Exit_fConversion
    
End Function
 

spacepro

Registered User.
Local time
Today, 00:22
Joined
Jan 13, 2003
Messages
715
it should work, take a look at the attachment. This is where the code should be.

Andy

Mile,

Thanks for that , wasn't too sure on the code.
 

Attachments

  • module.jpg
    module.jpg
    37.6 KB · Views: 196

Mile-O

Back once again...
Local time
Today, 00:22
Joined
Dec 10, 2002
Messages
11,316
It's not going to work fully, I suspect, as I've not seen the code for the second custom function nested in this one: fWidthRound - I'll best, however, that it also has references to the form that the code is attached to.
 

a.sinatra

quik
Local time
Yesterday, 16:22
Joined
Jan 10, 2004
Messages
262
Code:
Public Function fWidthRound(numToRound As Double) As Double
On Error GoTo Err_fWidthRound

Dim numDecimal As Double
Dim numIntPart As Double

Let numDecimal = numToRound - Fix(numToRound)
Let numIntPart = numToRound - numDecimal

Select Case numDecimal
    
    Case Is < 0.125
    Let fWidthRound = 0# + numIntPart

    Case Is < 0.375
    Let fWidthRound = 0.25 + numIntPart
    
    Case Is < 0.625
    Let fWidthRound = 0.5 + numIntPart
    
    Case Is < 0.875
    Let fWidthRound = 0.75 + numIntPart
    
    Case Is >= 0.875
    Let fWidthRound = 1# + numIntPart
    
End Select

Exit_fWidthRound:
    Exit Function
    
Err_fWidthRound:
    MsgBox Err.Description, , ErrTitle
    Resume Exit_fWidthRound

End Function

alright it calls it and comes up with an error though, how does the module know what it is converting? i need it to grab the number from "cmConversion1" and then place the output tp "cmConversion2"
________
Toyota In Motorsports
 
Last edited:

Mile-O

Back once again...
Local time
Today, 00:22
Joined
Dec 10, 2002
Messages
11,316
;)
 

Attachments

  • dbconversion.zip
    19.7 KB · Views: 190

a.sinatra

quik
Local time
Yesterday, 16:22
Joined
Jan 10, 2004
Messages
262
awesome, thank you to both you guys, this is what it looks like now...

Code:
Option Compare Database
Option Explicit

Public Function fWidthRound(numToRound As Double) As Double
On Error GoTo Err_fWidthRound

Dim numDecimal As Double
Dim numIntPart As Double

Let numDecimal = numToRound - Fix(numToRound)
Let numIntPart = numToRound - numDecimal

Select Case numDecimal
    
    Case Is < 0.125
    Let fWidthRound = 0# + numIntPart

    Case Is < 0.375
    Let fWidthRound = 0.25 + numIntPart
    
    Case Is < 0.625
    Let fWidthRound = 0.5 + numIntPart
    
    Case Is < 0.875
    Let fWidthRound = 0.75 + numIntPart
    
    Case Is >= 0.875
    Let fWidthRound = 1# + numIntPart
    
End Select

Exit_fWidthRound:
    Exit Function
    
Err_fWidthRound:
    MsgBox Err.Description, , ErrTitle
    Resume Exit_fWidthRound

End Function

Code:
Option Compare Database
Option Explicit

Public Function fConversion(ByVal varCentimetres As Variant) As Variant
On Error GoTo Err_fConversion

    Const cm = 0.393700787
    
    fConversion = fWidthRound(Left(varCentimetres * cm, 5))
    
Exit_fConversion:
    Exit Function

Err_fConversion:
    MsgBox Err.Description, , ErrTitle
    Resume Exit_fConversion
    
End Function

Code:
Private Sub convertCM_Click()
On Error GoTo Err_convertCM_Click

    'Checks to see if data is entered in first box
    If IsNumeric(Me.txtCentimeters) Then
        
        Dim convertCM As Double
        convertCM = Me.txtCentimeters
        Me.txtInches = fConversion(convertCM)
        
    Else

        'Displays error and resets everything, and gives focus to first box
        MsgBox "Please specify numbers only, letters and words cannot be converted.", vbInformation, ErrTitle
        Me.txtCentimeters = ""
        Me.txtInches = ""
        Me.txtCentimeters.SetFocus
        
    End If

Exit_convertCM_Click:
    Exit Sub
        
Err_convertCM_Click:
    MsgBox Err.Description, , ErrTitle
    Resume Exit_convertCM_Click
  
End Sub
________
STRAIN INDEX
 
Last edited:

spacepro

Registered User.
Local time
Today, 00:22
Joined
Jan 13, 2003
Messages
715
A.sinatra,

I think all the credit goes to Mile-O for this one.
(he knows is stuff)

HTH

Andy
 

Mile-O

Back once again...
Local time
Today, 00:22
Joined
Dec 10, 2002
Messages
11,316
What do you need that last sub for? You can make it update of its own accord (save's the user having to click a button) and any reference to Me. in a module not attached to a form is going to throw up an error.
 

Users who are viewing this thread

Top Bottom