Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating an If statement in VBA and calling it from design view

Posted on 2009-04-02
3
Medium Priority
?
329 Views
Last Modified: 2013-11-27
I need to write a pretty long and extensive If statement in MS Access which looks at the values of other fields and determines a code to give the record. The if statement has a lot of arguments based on various value combinations in the record.

I'm ok with the syntax of If, Else statements, what I really want advice on is how to structure that statement in a module and call it from design view.

Tried various ways but having a lot of trouble.

Any help please would be appreciated.
0
Comment
Question by:chris3vic
3 Comments
 
LVL 28

Expert Comment

by:TextReport
ID: 24048352
You will probably want to call it from an event procedure on the form such as AfterInsert or AfterUpdate, it may be that you need to call it from multiple events also.

If you need to do this from 1 event then put the code in the event procedure, if multiple then create the code in either the forms module or a global module and set the event(s) to =FunctionName()

Cheers, Andrew
0
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24048380
Hi chris3vic,

Have a look at the following:
http://www.classanytime.com/mis333k/sjsqlexercise1.html

Good Luck!

Gary
0
 
LVL 4

Accepted Solution

by:
Avelan earned 2000 total points
ID: 24048457
You can put the code in a function in a module and use the function in an expression in a query for example.
Not exactly sure what you mean by "design view" so don't know if this is what you need.

For example, if you want to test some conditions against column1 and column2 values and display the result in a third column
in a query, just create a function that takes two arguments and put in a call to it as an expression in the design view of a query.

Type the following in the query design view "Field" of the column you desire to show the result in
TestFunction([Table]![Col1],[Table]![Col2])

Assuming here your function is called TestFunction and you have columns named Col1 and Col2 from a table called Table shown also
on the query.

Not sure if this helps at all.

-Avelan
Function TestFunction(ByVal Value1 As Variant, ByVal Value2 As Variant) As String
    If IsNumeric(Value1) Then
        If IsNumeric(Value2) Then
            TestFunction = "Whee, they're both numeric"
        Else
            TestFunction = "The first one's numeric, the second isn't"
        End If
    Else
        TestFunction = "Blah, the first isn't numeric so i won't bother"
    End If
End Function

Open in new window

0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question