Results 1 to 10 of 10
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Runtime Error 2471

    I am trying to add a column to a table.



    It is easy if it is not there

    CurrentDb.Execute "ALTER TABLE TableName ADD COLUMN FieldName Type(Char length)"

    When it is there, then of course I error out. I am trying to set a function to check if it is there or not. If it is not there then make the Field and update from another source. If it is there, then only update from the other source.

    I think it is how I am referencing the function as I error out on the red Italicized, Bold and underlined code in the function. Am I calling the function incorrectly or am I putting in the incorrect information in the call code?

    See below

    Thanks

    Code:
    Public Sub AddColumn_Click()
    Dim strSQL As String
    
    strSQL = "UPDATE Data INNER JOIN Units ON Data.Unit = Units.Units SET Data.Netbase = [Units].[Netbase] WHERE (([Units].[Units]=[data].[Unit]));"
    
    Call FieldExist("Data", "Netbase")
        CurrentDb.Execute "ALTER TABLE Data ADD COLUMN Netbase Text(20)"
        DoCmd.RunSQL strSQL
        Exit Sub
    
    Call FieldExist("Data", "Netbase")
        DoCmd.RunSQL strSQL
    
    End Sub
    Function =
    Code:
    Public Function FieldExist(sField As String, sTable As String) As Boolean
       Err.Clear
       If (DCount(sTable, sField) = 0) And Err Then
            FieldExist = False
       Else: FieldExist = True
       End If
    End Function

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    Is the db split?

    why can't you just use manual table design?

  3. #3
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Because I am naturally lazy and prefer to do a click vs a multitude. Actually, the table gets replaced every week with an updated imported version. The extra column is to use as a reference to another table that does not even come close structurally. (Another product from another organization.)

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Can you use normal error handling, and for that error have it Resume Next instead of going to the error handler?

    http://www.baldyweb.com/ErrorTrap.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Thanks pbaldy. Your suggestion got rid of the function module.

    Code:
    Public Sub AddColumn_Click()
    Dim strSQL As String
    
    On Error GoTo Errorhandler
    
    strSQL = "UPDATE Data INNER JOIN Units ON Data.Unit = Units.Units SET Data.Netbase = [Units].[Netbase] WHERE (([Units].[Units]=[data].[Unit]));"
    
        CurrentDb.Execute "ALTER TABLE Data ADD COLUMN Netbase Text(20)"
        DoCmd.RunSQL strSQL
        Exit Sub
    
    ExitHandler:
        DoCmd.RunSQL strSQL
        Exit Sub
    
    Errorhandler:
            DoCmd.Hourglass False
            Resume ExitHandler
    End Sub
    It works as I wish, no errors popping up and updates both ways (No field or with field).

  6. #6
    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 was thinking more like:

    Code:
    Public Sub AddColumn_Click()
     Dim strSQL As String
    
    On Error GoTo Errorhandler
    
    strSQL = "UPDATE Data INNER JOIN Units ON Data.Unit = Units.Units SET Data.Netbase = [Units].[Netbase] WHERE (([Units].[Units]=[data].[Unit]));"
    
        CurrentDb.Execute "ALTER TABLE Data ADD COLUMN Netbase Text(20)"
        DoCmd.RunSQL strSQL
    
    ExitHandler:
        Exit Sub
    
    Errorhandler:
      Select Case Err
        Case 2471          'field already exists, ignore
          DoCmd.Hourglass False
          Resume Next
        Case Else
          MsgBox Err.Description & " in AddColumn_Click "
          DoCmd.Hourglass False
          Resume ExitHandler
      End Select
    End Sub
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    pbaldy,
    That gives me a pop up msg and doesn't update the column if it already exists.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Is the error number right? No big deal, you have something that works for you, but this is more "typical" error handling.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    I think the error came from the Function. You suggestion lessens the code needed. Thanks!

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem!
    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. Replies: 3
    Last Post: 02-26-2016, 12:34 PM
  2. Replies: 2
    Last Post: 08-22-2015, 11:26 AM
  3. Runtime 438 error
    By Thompyt in forum Programming
    Replies: 2
    Last Post: 06-04-2015, 10:35 AM
  4. Replies: 2
    Last Post: 10-15-2014, 04:23 AM
  5. Replies: 13
    Last Post: 06-12-2012, 09:52 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