diff --git a/DatabaseConn.vba b/DatabaseConn.vba index 8d5f81b..0b4a537 100644 --- a/DatabaseConn.vba +++ b/DatabaseConn.vba @@ -1,6 +1,6 @@ '----关闭数据库连接,释放资源 Public Sub closeConn() -On Error Resume Next +On Error Resume Next '错误仍继续执行 If conn Is Nothing Then Exit Sub conn.Close Set conn = Nothing @@ -13,25 +13,26 @@ Public Sub AutoRun() On Error Resume Next Dim strDBinf As String - Application.StatusBar = "正在连接数据库……" + Application.StatusBar = "正在连接数据库……" '在Excel状态栏上显示提示 If conn Is Nothing Then - Set conn = New ADODB.Connection + Set conn = New ADODB.Connection '初始化conn为一个新的ADO连接对象 Set RES = CreateObject("ADODB.Recordset") - strDBinf = "DSN=mysqlinklexcel32" '标准语句 - Call conn.Open(strDBinf) + strDBinf = "DSN=mysqlinklexcel32" '标准语句,因为在DSN中已经配置了IP、端口、用户名、密码等信息此处可不用再配置 + Call conn.Open(strDBinf) '尝试使用DSN链接数据库 - If VBA.Err.Number <> 0 Then + If VBA.Err.Number <> 0 Then '当VBA错误数量不为0时,则判断此时的DSN链接失败,提示使用64位的进行连接 strDBinf = "DSN=mysqlinklexcel64" '标准语句 - VBA.MsgBox "32位DSN连接失败,尝试使用mysqllinkexcel6连接!" + VBA.MsgBox "32位DSN连接失败,尝试使用mysqllinkexcel6连接!" Call conn.Open(strDBinf) End If - If VBA.Err.Number > 1 Then + If VBA.Err.Number > 1 Then '当VBA错误数量>1时,则判断此时的DSN链接失败 VBA.MsgBox "连接异常,请检查DB环境!" Set conn = Nothing End If + '当VBA错误数量<1时(也就是没有错误时),则判断此时的DSN链接成功 Application.StatusBar = "数据库连接成功" End If End Sub \ No newline at end of file