Solved

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

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Familiarize people with the process of utilizing SQL Server functions 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 Ac…
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…

828 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