Solved

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

Posted on 2009-04-02
3
320 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

943 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now