Results 1 to 7 of 7
  1. #1
    Zinger is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Oct 2015
    Posts
    7

    Lightbulb If and Dlookup

    Hi there!

    I am a newbie in MS Access VBA and my first time to post here, i am trying to update data from different tables using if condition and dlookup

    This is what i wrote:


    Private Sub Parameter_AfterUpdate()

    If ([Resul_tb]![ typeofsample].Value = "Sea water") Then



    VMA = DLookup("VMA", "tb_para", "ID =" & Parameter)

    Else
    MsgBox ("Something went wrong")

    End If

    End Sub

    Thanks in advance

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What is the issue? What happens - error message, wrong results, nothing?

    Why are you saving same data to multiple tables?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Welcome to the forum

    In addition to June's comments,
    you cannot reference a table like this
    Code:
    If ([Resul_tb]![ typeofsample].Value = "Sea water") Then
    Nowhere in your posted code is there a statement that will update anything.

    Usually you would use an update query or use VBA to open record sets and do the update.

  4. #4
    Zinger is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Oct 2015
    Posts
    7
    Click image for larger version. 

Name:	MS Access.png 
Views:	10 
Size:	25.6 KB 
ID:	22424
    i would like to avoid people entering same data everytime in VMA column.
    so i want to do something like this:

    if (resultados_tb.[typeofsample] = "Sea water") { run dlookup}
    elseif(resultados_tb.[typeofsample] = "Wastewater") { run dlookup}....

    How can i fix it?

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Is this a form/subform arrangement?

    What is Parameter - the combobox? Code is behind combobox? Combobox on main form?

    What is [Resul_tb]![ typeofsample]? Is this a field in the form's RecordSource?

    I still don't understand why you need to save the VMA value. If it can be retrieved by a DLookup using an ID as criteria, then it can be retrieved by query that joins tables.

    If you want to provide db for analysis, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    Zinger is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Oct 2015
    Posts
    7

    if and Dlookup

    Click image for larger version. 

Name:	MS Access.png 
Views:	9 
Size:	37.1 KB 
ID:	22430


    This is my dlookup code and it works perfectly without if condition


    Private Sub Parameter_AfterUpdate()

    Error line--> If ([resultados_tb].[typeofsample].Value = "Įgua de consumo") Then

    VMA = DLookup("VMA", "tb_para", "ID =" & Parameter) ....


    I would like to retrieve value using if condition because my table "tb_para" holds different values o VMA/VMR according to type of sample.


    This is how my table looks like.

    Sea water Wastewater
    ID | Parameter| VMA| VMR | VMA1|VMR1 |
    1 | pH | 8 | 8,5 | 5 | 7 |

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    As already explained, cannot refer directly to table like that in VBA code. If typeofsample is a field in the form's RecordSource or name of a control, simply:

    If Me.typeofsample = "something" Then
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. You could use (DLookup) instead of (IIf)
    By azhar2006 in forum Queries
    Replies: 7
    Last Post: 02-11-2014, 12:45 PM
  2. DLookUp
    By Subwind in forum Programming
    Replies: 4
    Last Post: 11-29-2013, 12:39 AM
  3. Like in Dlookup
    By msp4422 in forum Programming
    Replies: 3
    Last Post: 02-19-2013, 04:02 PM
  4. dlookup
    By ali zaib in forum Access
    Replies: 3
    Last Post: 01-13-2012, 11:57 AM
  5. DLookup help
    By denners05 in forum Access
    Replies: 1
    Last Post: 06-11-2011, 12:55 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