Results 1 to 2 of 2
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Posts
    254
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Update fields (Access 2000)

    I want to rewrite my update function in such a way, as to make it more flexible and easier to add on differenjt values.
    For the updating i have always 2 tables, products and products1 and i want to avoid writing always the name of the table before the name
    of the field. for example, instead of writing
    " products.office = products1.office" & _
    " , products.cons = products1.cons " & _
    " , products.oem = products1.oem "

    i want to write products(office,cons,oem) or something like that, but then i
    received Syntax error. Can somebody help me?

    The original function that works is the following


    Public Function UpdateFields()
    ' updates the fields Description, EndPrice and PerformanceLevel in the table Products
    Dim strSQLC As String
    strSQLC = " UPDATE products1 INNER JOIN products ON [products1].[productid]=[products].[productid] SET " & _
    " products.office = products1.office" & _
    " , products.cons = products1.cons " & _
    " , products.oem = products1.oem "
    CurrentDb.Execute strSQLC
    End Function

    The function i want to build but that gives syntax error is the following

    Public Function Test()
    Dim strSQLC As String
    strSQLC = " UPDATE products1 INNER JOIN products ON [products1].[productid]=[products].[productid] SET " & _
    " products(office,cons,oem)"
    CurrentDb.Execute strSQLC
    end function

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Update fields (Access 2000)

    You must enumerate the list of fields to be updated. Try this:

    Function UpdateFields(ParamArray Args())
    ' Updates the fields passed as parameters in the table Products
    Dim strSQLC As String
    Dim i As Integer

    ' Create the list of fields to be updated
    For i = LBound(Args) To UBound(Args)
    strSQLC = strSQLC & ", products.[" & Args(i) & "] = products1.[" & Args(i) & "]"
    Next i

    ' Remove first ", "
    If strSQLC <> "" Then
    strSQLC = Mid(strSQLC, 3)
    End If

    ' Create and execute complete SQL string
    strSQLC = " UPDATE products1 INNER JOIN products ON " & _
    "[products1].[productid]=[products].[productid] SET " & _
    strSQLC
    CurrentDb.Execute strSQLC
    End Function

    Call it as

    Call UpdateFields("office", "cons", "oem")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •