动态指定SQL Server名称和数据库失败:传递控件名而非实际值求助
问题:VBA动态指定SQL Server连接时传递控件名而非实际值
我尝试在VBA代码中动态指定SQL Server名称和数据库,但运行时传递的是控件名txtlink3而非实际服务器名称。附上运行后的服务器连接截图、数据源表单截图,同时提供了我编写的可正常运行的Access数据库链接代码作为参考,恳请协助解决问题。
有问题的SQL Server链接代码
Private Sub cmdlink1_Click() On Error GoTo Err_cmdlink1_Click DoCmd.SetWarnings False Dim strServer As String Dim strDatabase As String strServer = txtlink3 'This comes from a Field in the form strDatabase = txtlink4 'This comes from a Field in the form DoCmd.TransferDatabase acLink, "ODBC Database", "ODBC; Driver={SQL Server};Server=txtlink3;Database=txtlink4;Trusted_Connection=Yes", acTable, "dbo.address", "AddressMS" ' Use for test SQLdb on matts workstation MsgBox "Done" DoCmd.SetWarnings True Exit_cmdlink1_Click: Exit Sub Err_cmdlink1_Click: MsgBox Err.Description Resume Exit_cmdlink1_Click End Sub
可正常运行的Access数据库链接代码
Private Sub cmdlink2_Click() On Error GoTo Err_cmdlink2_Click DoCmd.SetWarnings False If DLookup("[license #]", "Ticket") <> DLookup("strlicense", "tblsmsettings") Or IsNull(DLookup("strlicense", "tblsmsettings")) = True Then MsgBox "You don't have a License to use this product. Please contact the software vendor", vbOKOnly Exit Sub 'be sure that they have a license to use this app End If Dim strFile As String strFile = txtlink2 If Dir(strFile) = "" Then ' see if the file exists before dropping MsgBox "The database specified does not exist" Else DoCmd.SetWarnings False DoCmd.RunSQL "drop Table [Journal Disbursements]" DoCmd.TransferDatabase acLink, "Microsoft Access", txtlink2, acTable, "Journal Disbursements", "Journal Disbursements" DoCmd.RunSQL "drop table [Journal Receipts]" DoCmd.TransferDatabase acLink, "Microsoft Access", txtlink2, acTable, "Journal Receipts", "Journal Receipts" DoCmd.RunSQL "drop table [Journal General]" DoCmd.TransferDatabase acLink, "Microsoft Access", txtlink2, acTable, "Journal General", "Journal General" DoCmd.RunSQL "drop table [Journal Purchases]" DoCmd.TransferDatabase acLink, "Microsoft Access", txtlink2, acTable, "Journal Purchases", "Journal Purchases" DoCmd.RunSQL "drop table [Journal Sales]" DoCmd.TransferDatabase acLink, "Microsoft Access", txtlink2, acTable, "Journal Sales", "Journal Sales" DoCmd.RunSQL "drop table [Ticket]" DoCmd.TransferDatabase acLink, "Microsoft Access", txtlink2, acTable, "Ticket", "Ticket" MsgBox "Done" DoCmd.SetWarnings True End If Exit_cmdlink2_Click: Exit Sub Err_cmdlink2_Click: MsgBox Err.Description Resume Exit_cmdlink2_Click End Sub
内容的提问来源于stack exchange,提问作者Matt
相关产品推荐
相关产品推荐

