Results 1 to 15 of 15
  1. #1
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23

    Create MS Access VBA Function for Multiple Conditions

    Hi,



    Im trying to create the following function in VBA for MS Access 2010.

    columnA columnB columnC
    A D French
    B E German
    C F

    Something like

    Function translateColumn(ColumnA as string, ColumnB as String)
    If Column A = "A" and ColumnB = "D" then translateColumn = "French"
    else if
    If Column A = "B" and ColumnB = "E" then translateColumn = "German"
    else if
    If Column A = "C" and ColumnB = "F" then translateColumn = "English"


    Can this be accomplised through VBA...

    Any help would is appreciated...

    thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sure, but it would make more sense to have the values in a table and simply look up the language. Much more flexible than having it hard coded in a function. You could do it the way you have it, but I'd probably use nested Select/Case, like this pseudo-code:

    Code:
    Select Case ColumnA
      Case "A"
        Select Case ColumnB
           Case "D"
              translateColumn = "French"
           Case "E"
    ...
      Case "B"
    ...
    But I'd have it all in a table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rzw0wr is offline I will always be a newbie
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Indiana
    Posts
    479
    Code:
    Function translateColumn(ColumnA as string, ColumnB as String)
    If Column A = "A" and ColumnB = "D"  then translateColumn = "French"
    else if
    If Column A = "B" and ColumnB = "E"  then translateColumn = "German"
    else if
    If Column A = "C" and ColumnB = "F"  then translateColumn = "English"
    Code:
    If ColumnA = "A" AND ColumnB = "D" then
          translateColumn = "French"
    ElseIf ColumnA = "B" AND CoulmnB = "E" then
          translateColumn = "German"
    ElseIf ColumnA = "C" AND ColumnB = "F" Then
          translateColumn = "English"
    End If
    EDIT: Paul's post is better an easier to troubleshoot.

    Dale

  4. #4
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23
    I've attached the associated Access File.

    I tried to add the function as a module. Afterwards, I use expression builder to create the Language Column.
    However, I can't get it to work.

    Im new at this and really need help.
    Attached Files Attached Files

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Just add it to the query (which I assume is where you want to use it):

    SELECT Sheet1.Type, Sheet1.Page, translateColumn([type],[page]) AS [Language]
    FROM Sheet1;
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23
    I added Paul's Module:

    Code:
    Function translateColumn(Domain As StringPage As String) As String
        Select 
    Case Domain
                  
    Case "A"
                         
    Select Case Page
                                   
    Case "D"
                                         
    translateColumn "English"
                  
    Case "B"
                         
    Select Case Page
                                   
    Case "E"
                                          
    translateColumn "French"
                
                
    End Select
    End Select
    End Select
    End 
    Function 
    But it stopped executing after the first row. Im not sure what im doing

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Does it help if I correct the indenting?

    Code:
      Select Case Domain
        Case "A"
          Select Case Page
            Case "D"
              translateColumn = "English"
            Case "B"
              Select Case Page
                Case "E"
                  translateColumn = "French"
    
              End Select
          End Select
      End Select
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23
    Click image for larger version. 

Name:	Module.png 
Views:	16 
Size:	50.1 KB 
ID:	12458Click image for larger version. 

Name:	Result.png 
Views:	14 
Size:	47.5 KB 
ID:	12459

    Results are shown above

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    So the corrected indenting didn't help? I don't think you understand the structure of Select/Case. Your test of "B" occurs as a test of the Page variable, not domain. You want this type of thing with Select/Case:

    Code:
    Function translateColumn1(Domain As String, Page As String) As String
      Select Case Domain
        Case "A"
          Select Case Page
            Case "D"
              translateColumn1 = "French"
            Case "E"
              translateColumn1 = "German"
            Case "F"
              translateColumn1 = "English"
          End Select
        Case "B"
          Select Case Page
            Case "E"
              translateColumn1 = "French"
          End Select
        Case "C"
          Select Case Page
            Case "F"
              translateColumn1 = "English"
          End Select
      End Select
    End Function
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23
    Works perfect now...you're the man Paul!

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help! But I'd still have the values in a table.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23
    Last question, for the Select Case, are we able to use wildcards?

    For example

    Instead of Case "A" , it would be Case like "*A*"

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I don't believe you can use Like in a Case statement. You could use the Left() function if you wanted to test the first character.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  14. #14
    nguyenak is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    May 2012
    Posts
    23
    then nested "if then" statement would be appropriate?

  15. #15
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Sure, if you want to keep it hard coded.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Multiple conditions when building
    By JustAccess in forum Queries
    Replies: 4
    Last Post: 04-29-2013, 12:26 PM
  2. Multiple conditions in Access
    By engr_saud1 in forum Access
    Replies: 1
    Last Post: 04-03-2013, 06:18 AM
  3. Replies: 3
    Last Post: 08-01-2012, 10:56 AM
  4. Using iif for Multiple Conditions
    By kwilbur in forum Access
    Replies: 5
    Last Post: 12-30-2011, 01:52 PM
  5. Expression with multiple conditions
    By techexpressinc in forum Queries
    Replies: 3
    Last Post: 06-19-2009, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums