When you say
open tables from a sql server and modify them
do you actually mean opening the (linked) table or running DDL SQL pass-through queries to modify the tables (that is how I would do it). Here is an example how to change bit fields in SQL to not accept NULL and set the default to 0 (to avoid the write conflict error):
Code:
Public Function vcCheckBoxesSetDefaultZero()
Dim tdf As DAO.TableDef, db As DAO.Database, fld As Field
Dim sTable As String, sField As String
Set db = CurrentDb
For Each tdf In db.TableDefs
If Left(tdf.Name, 4) <> "msys" Then
'only do SQL Server tables
If InStr(tdf.Connect, "SQL Server") > 0 Then
For Each fld In tdf.Fields
If fld.Type = dbBoolean Then
sTable = tdf.Name
sField = fld.Name
'call update to replace nulls with zeroes
Call vcAlterTable(tdf.Name, "UPDATE [" & sTable & "] SET [" & sField & "] = " & 0 & " WHERE [" & sField & "] IS NULL;", "tblAgents")
'now lets alter the table
Call vcAlterTable(tdf.Name, "ALTER TABLE [" & sTable & "] ALTER COLUMN [" & sField & "] BIT NOT NULL", "tblAgents")
Call vcAlterTable(tdf.Name, "ALTER TABLE [" & sTable & "] ADD CONSTRAINT DF_" & sTable & "_" & sField & " DEFAULT 0 FOR " & sField & ";", "tblAgents")
'Call vcAlterTable(tdf.Name, "ALTER TABLE [" & sTable & "] ALTER COLUMN [" & sField & "] BIT NOT NULL ;", "NAMES")
End If
Next fld
End If
End If
Next tdf
MsgBox "DONE"
End Function
Public Function vcAlterTable(strSourceTable As String, sAlterSQL As String, sUseConnectionStringFrom As String)
Dim db As DAO.Database
Dim qdExtData As QueryDef
Dim strSQL As String
On Error Resume Next
Set db = CurrentDb
If ObjectExists("QUERY", "sqlALTER_TABLE") Then DoCmd.DeleteObject acQuery, "sqlALTER_TABLE"
strSQL = sAlterSQL
Set qdExtData = db.CreateQueryDef("sqlALTER_TABLE")
qdExtData.Connect = CurrentDb.TableDefs(sUseConnectionStringFrom).Connect
qdExtData.ReturnsRecords = False
qdExtData.SQL = strSQL
CurrentDb.QueryDefs("sqlALTER_TABLE").Execute
'lets refresh the link
CurrentDb.TableDefs(strSourceTable).RefreshLink
qdExtData.Close
db.Close
Set db = Nothing
'MsgBox "ALTER TABLE STATEMENT COMPLETED - PLEASE CHECK THE TABLE IN DESIGN VIEW"
End Function
Cheers,