Home  Fees/Services  Microsoft Access Templates  Tutorials  Access Download  Articles  Search  Contact  Privacy  Links

Access VBA Tutorial
   
Access Programming Examples & Code Samples

VBA Select Case Visual Basic Tutorials, Workarounds & Solutions for VB6
Select Case VBA




VBA Tutorials:
Access to Google Earth
VB6 Age Calculation
Change Case VBA
VBA Email via Gmail
VBA Outlook Email
Read Email Access
RTF Report Email
Send Outlook Email Access
Calculate Running Sum
Concatenate Records
VBA Stock Quotes
VBA CreateQueryDef
Database Path Solution
Detail-Master Update
Field Validation VBA
Field Value New-Old
FindFirst Recordset
Get Version Number
VB6 Global Parameters
VBA Global Variables
Labels as Links
List Box Files List
VBA Mail Merge
OutputTo Crosstab
Sort Recordset Trick
VBA Recordset Filters
Reference Form Field
VBA Select Case
Transaction Processing

Visual Basic Function Examples


Select Case


VBA Select Case statement is one of the most useful Access VB tools used to program MS Access forms and reports.

The select case visual basic command takes the place of multiple nested if statements and makes your VB code clean and much easier to follow by neatly handling the conditional flow of your VBA programming code.

The syntax of the Access Case statement is:

Select Case Expression
    Case Expression_1
        Statement_1
    Case Expression_2
        Statement_2
    Case Expression_n
        Statement_n
    ...
End Select

Where Expression is a variable passed to the subroutine or a field on a form or within a recordset.

And Expression_1 to n are either constants, ranges of values, series of values, or calculations which resolve to a value for each Case.  You can also use alphanumeric comparisons in both the main Expression and the conditional Expressions (1..n).

In the VBA Select Case statement example below we have a report parameter form that has three choices for report output options:

1 - Label report
2 - Datasheet view of query results
3 - A series of statements to prepare an address file

Here is the example Access Case statement used in visual basic code:

Private Sub Command5_Click()
Dim pitney As String
Dim sqltext As String
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim stDocName As String

On Error GoTo Err_Command5_Click

Select Case Me.Output_Options
    Case 1
        stDocName = "R_Daily_Prospect_Labels"
        DoCmd.OpenReport stDocName, acPreview
    Case 2
        DoCmd.OpenQuery "Q_Daily_Prospect_Labels"
    Case 3
        Set db = CurrentDb
        pitney = DLookup("Path", "Q_path_Pitney", 1 = 1)
        pitney = Replace(pitney, "/", "\")
        pitney = Replace(pitney & "\", "\\", "\")
        pitney = pitney & "pitneybowes.mdb"
        DoCmd.RunSQL "delete * from mailmerge in '" & pitney & "'"

        sqltext = "INSERT INTO mailmerge (name,address1,address2,city,state,zip) in '" & pitney & "'" & _
        " select name,address1,address2,city,state,zip from q_daily_prospect_Pitney"

        DoCmd.RunSQL (sqltext)
        Set rst = db.OpenRecordset("select count(name) as icount from mailmerge in '" & pitney & "'")
        rst.MoveFirst
        MsgBox "A total of " & Nz(rst!Icount, 0) & " records were inserted into " & pitney
        rst.Close
        Set rst = Nothing
End Select

Call Mark_As_Sent_Click
Exit_Command5_Click:
Exit Sub

Err_Command5_Click:
MsgBox Err.Description
Resume Exit_Command5_Click

End Sub

More Information and Example for the VBA Select Case Statement:
 

Select Case in Dynamic Report

Select Case j pivot query dynamic crosstab report End Select skip_it: Next rst.Clone Set rst = Nothing End Sub ----------------------------------------- ...
www.blueclaw-db.com/report_dynamic_crosstab_field.htm


VBA Select Case in Proper Case Letters

Select Case Iasc(i) Case 65 To 90 ' uppercase letters If ispace = False Then ... End Select new_line = new_line & Chr(Iasc(i)) ' build the new line ...
www.blueclaw-db.com/change_name_proper_case.htm


Access Case Statement in Form Field Validation

Select Case Me.CC_Type ' combo box user selection field Case 1 'amex If IsNull(Me.CC_Number) = True Then MsgBox "You must enter an AMEX number." ...
www.blueclaw-db.com/formfieldvalidation.htm


VBA Select Case In Building Query Parameters

Select Case G_name Case "Project_ID" Get_Global=GBL_Project_ID ... End Select End Sub. In the form that causes the query (or report) to run, set the global ...
www.blueclaw-db.com/accessquerysql/parameter_query.htm

 








Have errors?  No doubt we can answer your question quickly and easily.  Simple questions get simple answers at no charge via email.




Popular Database Templates:

PM/Preventive Maintenance Access Template

Work Order Software

Real Estate Marketing Template Database




Contact Information

VBA runtime tips tricks fix solution

Access Visual Basic/VBA/VBScript/VB6 Tutorials
Visual Basic Error Fix & Problem Solutions