⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mdlpublic.bas

📁 对客户管理的系统 运行相应EXE文件前
💻 BAS
字号:
Attribute VB_Name = "mdlPublic"
Option Explicit

Public g_Conn As Connection
Public g_DBPath As String


Public Sub Main()
  g_DBPath = App.Path & "\Database\ManageClient.mdb"
  
  If ConnectToDatabase(DBAccess) = False Then
    Err.Raise vbObjectError + 1, , "连接数据库出错!|" + App.Path + "|"
  End If
End Sub


Public Function ConnectToDatabase(DBType As gxcDBType) As Boolean
  On Error GoTo ERR_CONN
  Set g_Conn = New Connection
  
  
  Dim ServerName As String, DBName As String, UserName As String, strPwd As String
  
  ServerName = "localhost"
  DBName = "ManageClient"
  UserName = "sa"
  strPwd = ""
  
  
  With g_Conn
     .CursorLocation = adUseClient
     .CommandTimeout = 10
     
     If DBType = DBAccess Then
      
        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
        "Data Source=" & g_DBPath
     Else
      
        .ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _
        "User ID=" & UserName & ";Initial Catalog=" & DBName & _
        ";Data Source=" & ServerName & ";pwd=" & strPwd
     End If
     .Open
  End With
  ConnectToDatabase = True
  Exit Function
  
ERR_CONN:
  ConnectToDatabase = False
  MsgBox Err.Description
  
End Function


Public Function RealString(strOrigional) As String
  RealString = Replace(strOrigional, "'", "''")
End Function


Public Function NextID(ByVal strTable As String, ByVal strID As String) As Long
  
  Dim rs As Recordset
  Set rs = g_Conn.Execute("SELECT MAX(" & strID & ") FROM " & strTable)
  
  If IsNull(rs(0)) Then
    
    NextID = 1
  Else
    
    NextID = rs(0).Value + 1
  End If
End Function


Public Function MaxID(ByVal strTable As String, ByVal strID As String) As Long
  
  Dim rs As Recordset
  Set rs = g_Conn.Execute("SELECT MAX(" & strID & ") FROM " & strTable)
  
  If IsNull(rs(0)) Then
    
    MaxID = 0
  Else
    
    MaxID = rs(0).Value
  End If
End Function



Public Function ExistByID(ByVal strTable As String, ByVal strID As String, _
                          ByVal lngID As Long) As Boolean
  
  Dim rs As Recordset
  Set rs = g_Conn.Execute("Select Count(*) from " & strTable & _
                          " where " & strID & "=" & lngID)
  ExistByID = (rs(0).Value = 1)
  
End Function


Public Function ExistByName(ByVal strTable As String, ByVal strFieldName As String, ByVal strName As String) As Boolean
  
  Dim rs As Recordset
  Set rs = g_Conn.Execute("Select Count(*) from " & strTable & " where " & strFieldName & "='" & strName & "'")
  ExistByName = (rs(0).Value = 1)
End Function



Public Function GetValueByID(ByVal strTable As String, ByVal strID As String, _
                  ByVal lngID As Long, ByVal strValueField As String) As String
  
  Dim rs As Recordset
  Set rs = g_Conn.Execute("Select " & strValueField & " from " & strTable & _
                          " where " & strID & "=" & lngID)
  If rs.RecordCount = 1 Then
    GetValueByID = rs(0).Value
  Else
    GetValueByID = ""
  End If
  Set rs = Nothing
  
End Function



Public Function RunSql(strSQL As String, ByRef strErrMsg As String) As Boolean
  
  On Error Resume Next
  
  g_Conn.Execute strSQL
  
  
  If Err.Number = 0 Then
    RunSql = True
  Else
    strErrMsg = Err.Description
    RunSql = False
  End If

End Function


Public Function GetRecordset(strSQL As String, ByRef strErrMsg As String, ByRef rs As Recordset) As Boolean
  
  On Error Resume Next
  
  Set rs = g_Conn.Execute(strSQL)
  
  
  If Err.Number = 0 Then
    GetRecordset = True
  Else
    strErrMsg = Err.Description
    GetRecordset = False
  End If

End Function


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -