通过VB查询失败(Infopath和Access)

时间:2013-04-18 20:50:41

标签: sql vb.net vba infopath2010 infopath-2007

我正在使用MS Access作为数据库的infopath表单。

在表单上,​​我想检查电子邮件是否有重复项,我试图通过在以下VB代码中运行SQL查询来完成此操作。

    Public Sub CTRL125_7_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)

    Dim myAdoConnection As AdoQueryConnection = _
    DirectCast(Me.DataConnections("Mainconnection"), AdoQueryConnection)


    Dim mynav As XPathNavigator = _
    CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:dataFields/d:Candidate_Tracking_Database/@Email", NamespaceManager)

    Dim email As String

    email = mynav.InnerXml

    myAdoConnection.Command = _
    "select [Email] from [Candidate_Tracking_Database] where [Email] =" + email

    myAdoConnection.Execute()
    End Sub

我想继续使用If,then,else语句来提示是否找到了副本。但是,当我尝试执行代码时出现以下错误:

System.Net.WebException was unhandled by user code
Message="The query method on the Document object failed.
Operation aborted"

我做错了什么?感谢您的回答。

注意:

我改编了以下代码 -     http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.adoqueryconnection.command.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

守则中的要素是:

Mainconnection = connection name
"/dfs:myFields/dfs:dataFields/d:Candidate_Tracking_Database/@Email" = Xpath of the input text box
[Email] = Field name in the [Candidate_Tracking_Database] table

完整错误是:

System.Net.WebException was unhandled by user code
Message="The query method on the Document object failed.
Operation aborted
"
Source="Microsoft.Office.InfoPath.Client.Internal.Host"
StackTrace:
   at      Microsoft.Office.InfoPath.Internal.MomExceptionHelper.ExecuteDataConnectionAction(OMCall d)
   at Microsoft.Office.InfoPath.Internal.AdoQueryConnectionHost.Execute()
   at Candidate_Registration.FormCode.CTRL125_7_Clicked(Object sender, ClickedEventArgs e) in C:\Infopath\InfoPath Projects\Candidate Registration\FormCode.vb:line 300
   at Microsoft.Office.InfoPath.Internal.ButtonEventHost.OnButtonClick(DocActionEvent pEvent)
   at Microsoft.Office.Interop.InfoPath.SemiTrust._ButtonEventSink_SinkHelper.OnClick(DocActionEvent pEvent)

1 个答案:

答案 0 :(得分:0)

试试这个:

myAdoConnection.Command = _ "select [Email] from [Candidate_Tracking_Database] where [Email] =" & email

或者这个:

myAdoConnection.Command = _ "select [Email] from [Candidate_Tracking_Database] where [Email] ='" & email & "'"