Home > Mobile >  Call vb class to form load
Call vb class to form load

Time:05-19

I am trying to move the following code to the VB Class, let says DataAccess.vb and then call the created VB Class using button click event, but I don't know how to do that. Does anyone here would help me to find out how to do that.

This is the code that I need to move to VB Class.

Sub LoadData()
    Try
        LblInvId.Text = Me._InvID.Substring(57, 27).Trim
        Dim InvID As String = LblInvId.Text.Trim
        Using cn As New SqlClient.SqlConnection(DatabaseConnectionForPDA.DatabaseConnection.CONNECT_RO_FOR_HMCS)
            cn.Open()
            Dim sql As String = String.Empty
            sql &= " SELECT T1.InvID "
            sql &= "    ,   T1.ItemID"
            sql &= "    ,   T2.ItemRevNo"
            sql &= "    ,   T1.RefNo2"
            sql &= "    ,   SUM(T1.Qty) AS Qty"
            sql &= "    ,   COUNT(*) AS CRow"
            sql &= " FROM [HMCS].dbo.R_Warehouse T1 "
            sql &= " INNER JOIN [HMCS].dbo.M_Item T2 "
            sql &= " ON T1.ItemID = T2.ItemID "
            sql &= " WHERE T1.InvDate = [HMCS].dbo.f_GetInvDate() "
            sql &= " AND (RTRIM(T1.InvID) = @InvID OR RTRIM(T1.RefNo2) = @InvID)"
            sql &= " GROUP BY T1.InvID, T1.ItemID, T2.ItemRevNo, T1.RefNo2"

            Using cmd As New SqlClient.SqlCommand(sql, cn)
                cmd.Parameters.AddWithValue("@InvID", InvID) 
                Using dr As SqlDataReader = cmd.ExecuteReader()
                    Do Until dr.Read = False
                        LblItmNo.Text = dr("ItemID")
                        LblItmRevNo.Text = dr("ItemRevNo")
                        LblRevNo2.Text = dr("RefNo2")
                        LblQty.Text = dr("Qty")
                        LblRow.Text = dr("CRow")
                        Exit Do
                    Loop
                End Using
            End Using
            cn.Close()
            If LblRow.Text = "" Then
                err.Display("在庫レコードが見つかりません", "Error Message")
                Me.Close()
            End If
        End Using
    Catch ex As Exception
        err.Display(ex.Message, "Error Message")
    End Try

End Sub

CodePudding user response:

You can achieve that by creating an OnClick Event for the Button and referring to your function on the code side.

DataAccess.LoadData()

You will also need to pass the Textfields to the function since it will not know what for example LblInvId is in the separate class.

DataAccess.LoadData(LblInvId,...)

CodePudding user response:

By assuming that you meant calling LoadData() from an event handler in another class, you have at least 2 options. One, making LoadData() shared then calling it as DataAccess.LoadData() in your click event handler. Two, creating an instance of DataAccess to access LoadData(). In your event handler:

Dim da As New DataAccess()
da.LoadData()
  • Related