有人可以给我看一个简单的vb.net样本,将客户添加到quickbooks

时间:2013-07-11 22:07:02

标签: wpf vb.net sample intuit

我正在尝试编写一个VB.net WPF程序,它将向客户添加自定义注释。 Intuit让我围绕他们的代码示例,不会从C#转换为VB.net。

有人可以向我展示一个非常简单的示例,仅显示添加客户名称吗?省略任何错误检查等,我可以搞清楚。我只是不知道如何编写与quickbooks的连接并发送请求。也许是一个带有文本框和按钮的简单表单,其功能是将名称放在文本框中,然后单击按钮添加客户名称。

2 个答案:

答案 0 :(得分:0)

我一直对来自Intuit for Quickbooks SDK的糟糕代码示例感到厌恶。话虽如此,我仍然使用它们;修复代码后,它们非常有用。

您还需要添加对Intuit Quickbooks SDK QBFC7Lib的引用;根据版本的不同,它可能会略有不同。

以下是使代码有用所需的操作(使用编辑>快速替换):

  1. //替换为'(评论)
  2. 删除所有;(分号)
  3. ==替换为=
  4. For j = 0替换为For j as Integer = 0
  5. responseList.GetAt(i)替换为responseList.GetAt(j)
  6. 删除尾随.(点)
  7. Integerfor替换为Integer(下一行)for
  8. DataExtRet替换为DataExtRet.(仅当它显示错误时)
  9. 检查是否存在任何其他错误
  10. 删除不需要的内容
  11. 当您第一次运行它时,您需要打开Quickbooks并准备好从Quickbooks授予权限。这是一个修复错误的示例。

    Imports QBFC7Lib
    
    Module QBSample
    
    Public Class Customer
    
        Public Sub DoCustomerAdd()
            Dim sessionBegun As Boolean
            sessionBegun = False
            Dim connectionOpen As Boolean
            connectionOpen = False
            Dim sessionManager As QBSessionManager
            sessionManager = Nothing
    
            Try
    
                'Create the session Manager object
                sessionManager = New QBSessionManager
    
                'Create the message set request object to hold our request
                Dim requestMsgSet As IMsgSetRequest
                requestMsgSet = sessionManager.CreateMsgSetRequest("US", 7, 0)
                requestMsgSet.Attributes.OnError = ENRqOnError.roeContinue
    
                BuildCustomerAddRq(requestMsgSet)
    
                'Connect to QuickBooks and begin a session
                sessionManager.OpenConnection("", "Your application")
                connectionOpen = True
                sessionManager.BeginSession("", ENOpenMode.omDontCare)
                sessionBegun = True
    
                'Send the request and get the response from QuickBooks
                Dim responseMsgSet As IMsgSetResponse
                responseMsgSet = sessionManager.DoRequests(requestMsgSet)
    
                'End the session and close the connection to QuickBooks
                sessionManager.EndSession()
                sessionBegun = False
                sessionManager.CloseConnection()
                connectionOpen = False
    
                WalkCustomerAddRs(responseMsgSet)
            Catch e As Exception
                MessageBox.Show(e.Message, "Error")
                If (sessionBegun) Then
                    sessionManager.EndSession()
                End If
                If (connectionOpen) Then
                    sessionManager.CloseConnection()
                End If
            End Try
        End Sub
        Private Sub BuildCustomerAddRq(ByVal requestMsgSet As IMsgSetRequest)
            Dim CustomerAddRq As ICustomerAdd
            CustomerAddRq = requestMsgSet.AppendCustomerAddRq()
            'Set field value for Name
            CustomerAddRq.Name.SetValue("Test Customer 2")
            'Set field value for IsActive
            CustomerAddRq.IsActive.SetValue(True)
            'Set field value for CompanyName
            CustomerAddRq.CompanyName.SetValue("ab")
            'Set field value for Salutation
            CustomerAddRq.Salutation.SetValue("ab")
            'Set field value for FirstName
            CustomerAddRq.FirstName.SetValue("ab")
            'Set field value for MiddleName
            CustomerAddRq.MiddleName.SetValue("ab")
            'Set field value for LastName
            CustomerAddRq.LastName.SetValue("ab")
            'Set field value for Addr1
            CustomerAddRq.BillAddress.Addr1.SetValue("ab")
            'Set field value for Addr2
            CustomerAddRq.BillAddress.Addr2.SetValue("ab")
            'Set field value for Addr3
            CustomerAddRq.BillAddress.Addr3.SetValue("ab")
            'Set field value for Addr4
            CustomerAddRq.BillAddress.Addr4.SetValue("ab")
            'Set field value for Phone
            CustomerAddRq.Phone.SetValue("ab")
            'Set field value for AltPhone
            CustomerAddRq.AltPhone.SetValue("ab")
            'Set field value for Fax
            CustomerAddRq.Fax.SetValue("ab")
            'Set field value for Email
            CustomerAddRq.Email.SetValue("ab")
            'Set field value for Contact
            CustomerAddRq.Contact.SetValue("ab")
            'Set field value for AltContact
            CustomerAddRq.AltContact.SetValue("ab")
    
            'May create more than one of these if needed
            CustomerAddRq.IncludeRetElementList.Add("Name")
            CustomerAddRq.IncludeRetElementList.Add("ListID")
    
        End Sub
    
        Private Sub WalkCustomerAddRs(ByVal responseMsgSet As IMsgSetResponse)
            If (responseMsgSet Is Nothing) Then
                Exit Sub
            End If
    
            Dim responseList As IResponseList
            responseList = responseMsgSet.ResponseList
            If (responseList Is Nothing) Then
                Exit Sub
            End If
    
            'if we sent only one request, there is only one response, we'll walk the list for this sample
            For j As Integer = 0 To responseList.Count - 1
                Dim response As IResponse
                response = responseList.GetAt(j)
                'check the status code of the response, 0=ok, >0 is warning
                If (response.StatusCode >= 0) Then
                    'the request-specific response is in the details, make sure we have some
                    If (Not response.Detail Is Nothing) Then
                        'make sure the response is the type we're expecting
                        Dim responseType As ENResponseType
                        responseType = CType(response.Type.GetValue(), ENResponseType)
                        If (responseType = ENResponseType.rtCustomerAddRs) Then
                            ''upcast to more specific type here, this is safe because we checked with response.Type check above
                            Dim CustomerRet As ICustomerRet
                            CustomerRet = CType(response.Detail, ICustomerRet)
                            WalkCustomerRet(CustomerRet)
                        End If
                    End If
                End If
            Next j
        End Sub
    
        Private Sub WalkCustomerRet(ByVal CustomerRet As ICustomerRet)
            If (CustomerRet Is Nothing) Then
                Exit Sub
            End If
    
            'Go through all the elements of ICustomerRet
            'Get value of ListID
            Dim ListID1 As String
            ListID1 = CustomerRet.ListID.GetValue()
            'Get value of Name
            Dim Name5 As String
            Name5 = CustomerRet.Name.GetValue()
    
        End Sub
    
    End Class
    
    End Module
    

    我经常看到的另一个问题是代码从“retlist”跳到“ret”。这通常发生在例如查询中ReceivePaymentQuery。这不是DoCustomerAdd的问题。在转换其他方法时,我只在此提及它以供参考。

    来自OSR的代码:

    Public Sub WalkReceivePaymentRet(ByVal ReceivePaymentRet As IReceivePaymentRetList)
        If (ReceivePaymentRet Is Nothing) Then
            Exit Sub
        End If
    

    修复如下:

    Public Sub WalkReceivePaymentRet(ByVal ReceivePaymentRetList As IReceivePaymentRetList)
    
        For a As Integer = 0 To ReceivePaymentRetList.Count - 1
            Dim ReceivePaymentRet As IReceivePaymentRet
    
            ReceivePaymentRet = ReceivePaymentRetList.GetAt(a)
    
            If (ReceivePaymentRet Is Nothing) Then
                Exit Sub
            End If
    

答案 1 :(得分:0)

通过执行以下操作,确保将“ AddCu”添加到Quickbook中:

QB->编辑->首选项->集成应用程序->公司首选项

允许访问:✓|应用名称:AddCu

导入Interop.QBFC13

Public Sub AddCu()
    ' Create a QBSessionManager object:
    Dim SessionManager As QBSessionManager
    SessionManager = New QBSessionManager

    ' Create an IMsgSetRequest object (the parameters specify US QuickBooks and the 6.0 spec):
    Dim CustomerSet As IMsgSetRequest
    CustomerSet = SessionManager.CreateMsgSetRequest("US", 6, 0)

    ' Create an ICustomerAdd object:
    Dim customer As ICustomerAdd

    customer = CustomerSet.AppendCustomerAddRq

    ' Set the ICustomerAdd object's field values:
    customer.Name.SetValue("Joey Hamham")

    customer.Phone.SetValue("315-253-2642")

    customer.Email.SetValue("bla@gmail")

    ' The request message set for this example is complete
    ' Open a connection:
    SessionManager.OpenConnection("App", "AddCu")
    ' Begin a session:
    SessionManager.BeginSession("", ENOpenMode.omDontCare)

    ' Create a IMsgSetResponse object:
    Dim Resp As IMsgSetResponse

    ' Call DoRequests, passing the IMsgSetRequest object that was populated with a 
    Resp = SessionManager.DoRequests(CustomerSet)

    ' Display the unprocessed QBXML in the response in a message box:
    MsgBox(Resp.ToXMLString)

    ' End the session:
    SessionManager.EndSession()
    ' Close the connection:
    SessionManager.CloseConnection()
    ' Clear the QBSessionManager
    SessionManager = Nothing
End Sub