简体   繁体   中英

Schema changes not updated in MS Access front end linked to the database

I created a new table in SQL server using SQL Server Management Studio but the MS Access front end linked to the database was not updated.

I tried reopening Access but still the new tables cannot be found. Yet when I check the SQL Server database they are there.

My tables in Access are linked to the database so I assumed any table or changes made in the SQL server database would be reflected in the Access front end . When I run a query in Access looking for the tables nothing is found. Another bit of information is when I right click and press view dependencies it says unable to view dependencies because

"unable to cast object of type 'System.DBNull' to type 'System.string'"

Something is maybe wrong with the way i save the query but I am not sure.

Your assumption:

I assumed any table or changes made in the SQL server database would be reflected in the Access front end

... is not correct. Access does not automatically re-link when the SQL Server's schema changes, and it really can't. You're expecting Access assumes the data models between SQL Server and Access are the same. Even if your table and column names are exactly the same there are still differences to deal with since the data types have some differences. So, even in the best-case scenario Access does not have enough info to automatically re-link.

When you modify the SQL Server db you have to re-link from Access. Here's an article with some code that will allow you to do that quickly but note that you still have to launch it manually. And beware, as mentioned above, linking isn't that straightforward. If you use an automated method for linking the process will have to make some decisions, some of which make take you by surprise.

I have found the management of linked tables in access to be administratively tedious. In order to make my life simpler I have used the functions below that can be called to update the linked tables in access. This will take care of updating the structure of any changed table in SQL. Adding values to the SetTableNames function will bring in new tables

Private mstrTableNames(100) As String
Private const gcSQLDB as string = "MySQLServer"
Private const gcUserID as string = "SQLUserName"
Private const gcUserPassword as string = "SQLPassword"
Private const gcLiveDSN as string = "DSN"
Private const gcEmpty as string = ""

Public Function LinkLiveTables() As Boolean

   Dim tdfLinked As TableDef
   Dim strConnect As String
   Dim intLoop As Integer

   'Remove all non system tables from the application: 
   ' !!!NB Add other exclusions so as to not delete tables that are not linked!!!
    For Each tdfLinked In CurrentDb.TableDefs
      If Left(tdfLinked.Name, 2) <> "MS" Then
        If Left(tdfLinked.Name, 7) <> "tblTemp" Then
         CurrentDb.TableDefs.Delete tdfLinked.Name
        End If
      End If
   Next

   'Create a linked table that points to SQL Server
   strConnect = "ODBC;DATABASE=" & gcSQLDB & ";UID=" & gcUserID & _
                ";PWD=" & gcUserPassword & ";DSN=" & gcLiveDSN
   SetTablesNames
   For intLoop = 1 To 100
      If mstrTableNames(intLoop) = gcEmpty Then GoTo ProcExit
      Set tdfLinked = CurrentDb.CreateTableDef(mstrTableNames(intLoop))
      With tdfLinked
         .Connect = strConnect
         .SourceTableName = "dbo." & mstrTableNames(intLoop)
      End With
      CurrentDb.TableDefs.Append tdfLinked
   Next

ProcExit:
   MsgBox "Connection to the LIVE tables was successful.", vbInformation
   Exit Function
ProcError:
   MsgBox "Link to LIVE tables Failed." & vbCrLf & vbCrLf & _
          "Error Number : " & Err.number & vbCrLf & _
          "Error Description : " & Err.Description, vbCritical
End Function

Private Sub SetTablesNames()

   mstrTableNames(1) = "tblMoistureHist"
   mstrTableNames(2) = "tblRawMaterials"
    ' ... add the additional table that you need as mstrTableNames(n) = "tablename"
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM