教教我吧,ini写入文件
我想写一个SourceDB.ini在模块中部分代码如下:
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public conn As Connection
Public rs As Recordset
Public DBConnectString As String
Public ServerName As String
Public ServerLogin As String
Public ServerPass As String
Public ServerDbName As String
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String) As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
Dim GetStr As String
GetStr = VBA.String(128, 0)
'GetPrivateProfileString AppName, In_Key, "", GetStr, 256, App.Path & "\SourceDB.ini"
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, "E:\lvming\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End Function
'写INI文件
Public Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Data) = "" Or VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
'WritePrivateProfileString AppName, In_Key, In_Data, App.Path & "\SourceDB.ini"
WritePrivateProfileString AppName, In_Key, In_Data, "E:\lvming\SourceDB.ini"
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
Function Db_open() As Boolean
On Error GoTo ErrorHandler
Dim dbstr As String
Dim ComputerName As String * 256
GetComputerName ComputerName, 255
' dbstr = "Provider=SQLOLEDB.1;Password=159753;Persist Security Info=True;User ID=sa;Initial Catalog=v_catch;Data Source=test"
dbstr = "Provider=SQLOLEDB.1;Password=" & ServerPass & ";Persist Security Info=True;User ID=" & ServerLogin & ";Initial Catalog=" & ServerDbName & ";Data Source=" & ServerName & ""
End If
DBConnectString = dbstr
Set conn = New Connection
conn.Open dbstr
If conn.State = 1 Then
Db_open = True
Exit Function
End If
ErrorHandler:
Db_open = False
End Function
Sub db_close()
On Error Resume Next
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
Public Sub EndProgram(Frm As Form)
On Error Resume Next
Call db_close
Unload Frm
End
End Sub
Private Sub Form_Load()
ServerName = GetIniStr("SQL", "ServerName")
ServerLogin = GetIniStr("SQL", "Login")
ServerPass = GetIniStr("SQL", "password")
ServerDbName = GetIniStr("SQL", "ServerDbName")
...
...
我想在数据备份的form中调用
部分代码如下:
Dim sql, workarea(12) As String
ProgressBar1.Visible = True
ProgressBar1.Min = LBound(workarea)
ProgressBar1.max = UBound(workarea)
ProgressBar1.Value = ProgressBar1.Min
For connter = LBound(workarea) To UBound(workarea)
workarea(connter) = "initial value" & connter
ProgressBar1.Value = connter
cnn.Open "provider=sqloledb.1;persist security info = false;data source=test;initial catalog=v_catch;user id=sa;password=159753" 通过模块的使用让这语句不需要
sql = "backup DATABASE v_catch TO disk='" & Text1.Text & "'" 把v_catch 改成"& serverdbname &"
Debug.Print sql
cnn.Execute (sql) '执行SQL语句
cnn.Close 通过模块的使用让这语句不需要
Next connter
ProgressBar1.Value = ProgressBar1.Min
MsgBox "数据库备份成功!!", 64, "提示信息"
请问在我的模块中怎么写