위의 질문에서 설명한 문제를 해결하기 위해 두 가지 다른 작업을 수행했습니다. 먼저 테이블 이름을 바꾸는 루틴을 만들었습니다. 나중에 나는 이것을 버리기로 결심했다. 데이터베이스가 시작될 때 테이블 연결을 처리하는 다른 루틴 (아래에 나열)을 작성했다.
Public Sub subChangeLinkedTableNames()
Dim dbCurr As DAO.Database
Dim tdfCurr As DAO.TableDef
Set dbCurr = CurrentDb()
For Each tdfCurr In dbCurr.TableDefs
If Len(tdfCurr.Connect) > 0 Then
If Left(tdfCurr.Name, 4) = "dbo_" Then
tdfCurr.Name = Replace(tdfCurr.Name, "dbo_", "")
End If
End If
Next
Set tdfCurr = Nothing
Set dbCurr = Nothing
End Sub
위의 코드는 잘 작동하지만 결국 나는 테이블 내 Access 데이터베이스를 열 때마다 다시 링크 자동화 루틴을 쓰기로했다. 이 루틴은 링크 될 테이블 목록을 반복하며 각 테이블에 대해이 하위를 호출합니다. 나는 테이블 sLocalTableName라는 변수/인수에 갖고 싶은 이름을 지정하여 문제를 명명 테이블을 해결하고있어 알 수 있습니다 :
Private Sub LinkODBCTable(sSourceTableName As String, sLocalTableName As String, sIndexFields As String, sConString As String)
Dim dbCurrent As DAO.Database
Dim tdfCurrent As DAO.TableDef
Set dbCurrent = DBEngine.Workspaces(0).Databases(0)
On Error Resume Next
'Let's not accidentally delete a local table of the same name
If Len(dbCurrent.TableDefs(sLocalTableName).Connect) > 0 Then
dbCurrent.TableDefs.Delete sLocalTableName
End If
Select Case Err.Number
Case 0
'Do Nothing
Case Else
Err.Clear
'Case 3011
'Table does not exist
End Select
Set tdfCurrent = dbCurrent.CreateTableDef(sLocalTableName)
tdfCurrent.Connect = sConString
tdfCurrent.SourceTableName = sSourceTableName
dbCurrent.TableDefs.Append tdfCurrent
If Err.Number <> 0 Then
'Sometimes 3010 occurs here and I don't know why. A compact and repair always seems to fix it.
MsgBox "Error in LinkODBCTable" & vbCrLf & vbCrLf & Err.Number & " " & Err.description
Err.Clear
End If
If sIndexFields <> "" Then
'sIndexFields should be field names, each enclosed in brackets, comma separated
'Most of the time it will just be one field
'This is to tell Access which field(s) is the Primary Key
dbCurrent.Execute "CREATE INDEX __UniqueIndex ON [" & sLocalTableName & "] (" & sIndexFields & ")", dbFailOnError
If Err.Number <> 0 Then
If Err.Number = 3283 Then
'Primary Key Already Exists
Else
MsgBox "Error in LinkODBCTable" & vbCrLf & vbCrLf & Err.Number & " " & Err.description
End If
Err.Clear
End If
End If
Set tdfCurrent = Nothing
Set dbCurrent = Nothing
End Sub
출처
2012-04-25 20:18:24
HK1
당신은 전혀 쿼리에 테이블 이름을 바꿀 필요가 없습니다. –
그게 내가 생각한거야하지만 나는 액세스 및 DSN을 사용하여 ODBC를 통해 링크 된 양식을 사용하고 있으므로 정말 연결된 테이블의 이름을 제어 할 수 없어. – HK1
"연결된 테이블의 이름 제어"가 없다는 것은 무엇을 의미합니까? 아니요, SQL Server 테이블의 이름을 제어 할 수는 없지만 액세스 프런트 엔드에서 연결된 테이블의 이름을 완벽하게 제어 할 수는 있습니다. 링크 할 때 기본 이름은 "dbo"를 포함하지만 링크 된 테이블의 이름을 바꾸면됩니다. –