我有一个ms访问数据库,我需要在不同的计算机上运行。我的问题是每台计算机都将后端存储在不同的位置。到目前为止,我只有2台机器在运行,但我知道我需要运行更多机器。我将每台计算机的位置存储在一个公共常量中,并使用简单的If语句将其链接到正确的一个。
Public Const strFolderDatabasePc1 as string "c:\DatabasePc1"
Public Const strFolderDatabasePc2 as string "c:\DatabasePc2"
....
是否可以在数据库加载时使用if语句创建Public Const?
Public Const strFolderDatabase as string
If Pc1 then
strFolderDatabase = FolderPc1
else if Pc2 then
strFolderDatabase = FolderPc2
else if Pc3 then
strFolderDatabase = FolderPc3
else
strError
EndIf
谢谢。
更新:
到目前为止,我已经编写了以下代码。它包含4个用户。
'Database Folders
Public Const MainFolder As String = "\\localhost\c$\User\Main"
Public Const UserAFolder As String = "\\localhost\c$\User\UserA"
Public Const UserBFolder As String = "\\localhost\c$\User\UserB"
Public Function AdminFolder() As String
AdminFolder = Application.CurrentProject.Path & "\Admin\"
End Function
'Database Keys
Public Const MainUserKey As String = "\\localhost\c$\User\Main\Key.txt"
Public Const UserAKey As String = "\\localhost\c$\User\UserA\key.txt"
Public Const UserBKey As String = "\\localhost\c$\User\UserB\key.txt"
Public Function AdminKey() As String
AdminFolder = Application.CurrentProject.Path & "\Admin\key.txt"
End Function
Public Function FolderExists(ByVal path_ As String) As Boolean
On Error Resume Next
FolderExists = (GetAttr(path_) And vbDirectory) = vbDirectory
On Error GoTo 0
End Function
Public Function FileExists(ByVal path_ As String) As Boolean
On Error Resume Next
FileExists = (Len(Dir(path_)) > 0)
On Error GoTo 0
End Function
Public Function FolderDatabase()
If FileExists(AdminKey) And FolderExists(AdminFolder) Then
'Admin
FolderDatabase = AdminFolder
ElseIf FileExists(MainUserKey) And FolderExists(UserMainFolder) Then
'MainUser
FolderDatabase = UserMainFolder
ElseIf FileExists(UserAKey) And FolderExists(UserAFolder) Then
'UserA
FolderDatabase = UserAFolder
ElseIf FileExists(UserBKey) And FolderExists(UserBFolder) Then
'UserB
FolderDatabase = UserBFolder
Else
'Unknown User
'Do something else...
End If
End Function
在每个表单中使用以下代码。
Sub Check()
If FolderExists(FolderDatabase) Then
'===> User, Continue Loading.
If Dir(FolderDatabase & "*.*") = "" Then
'===> Empty Folder.
'Do something...
Else
'===> Files On Folder.
'Do something...
End If
Else
'===> Not Known User.
Application.Quit acQuitSaveNone
End If
End Sub
我可以用更简单的方式编写或执行此操作吗?谢谢。
PS: 我需要在一个地方拥有每个USER的位置,因为我将来可能会更改位置或文件名。此外,我正在使用另外两个不同名称的文件夹,我再次使用上述所有文件夹。
答案 0 :(得分:2)
简单来说就是no,但是你可以让一个函数通过传递pc号作为参数来返回正确的路径:
Public Const strFolderDatabasePc1 as string "c:\DatabasePc1"
Public Const strFolderDatabasePc2 as string "c:\DatabasePc2"
Public Function FolderDatabase(ByVal pcNo As Long) As String
Select Case pcNo
Case 1:
FolderDatabase = strFolderDatabasePc1
Case 2:
FolderDatabase = strFolderDatabasePc2
End Select
End Function
然后只需传递所需的电脑号码:
Dim path_ As String
path_ = FolderDatabase(1)
为了使其更具可读性,请为各种电脑设置Enum
。
Public Enum Computers
Home
Work
End Enum
Public Function FolderDatabase(ByVal pc As Computers) As String
Select Case pc
Case Computers.Home:
FolderDatabase = strFolderDatabasePc1
Case Computers.Work:
FolderDatabase = strFolderDatabasePc2
End Select
End Function
Dim path_ As String
path_ = FolderDatabase(Computers.Home)
答案 1 :(得分:1)
没有意义。每台计算机都有自己的C:
驱动器,其他人不会看到。
所以你可以简单地使用:
Public Const strFolderDatabasePc As string "c:\DatabasePc"
也就是说,您可能最好使用C:\Users\Public
下的文件夹,因为您的数据库文件不是,如Windows所见,应用程序只是文档。 / p>