I want to check if txtCRCNum1 is empty or not. If it is empty, then some values from frmComm will get displayed on frmcrc (which I call the default values). If not empty, then it will execute an SQL query which will select data from the value of txtCRCNum1 and will display the corresponding data to frmcrc. Here's my code:

Private Sub frmcrc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


            If String.IsNullOrEmpty(frmComm.txtCRCNum1.Text) Then 'if txtCRCNum1 is empty

                'display default values

                ClearTextBox(Me)


                txtCLTAcctNo.Text = frmComm.cmbAcctNo1.Text
                txtCRLNum.Text = frmComm.txtCRLNo.Text
                txtProjName.Text = frmComm.txtUnitCode1.Text
                txtClientName.Text = frmComm.cmbClientName1.Text + " " + frmComm.Label13.Text

                txtCommType.Text = frmComm.cmbParticulars1.Text
                txtPayeeName.Text = frmComm.cmbPayee01.Text
                Me.lblrootpayee.Text = frmComm.lblrootpayee.Text

            Else 'else retrieve data based from given crc num

                conn = GetConnect()
                Dim com As New SqlClient.SqlCommand
                Dim dr As SqlClient.SqlDataReader

                conn.Open()
                com.CommandText = "Select * from tblCRClearance where crc_no = '" & frmComm.txtCRCNum1.Text & "' and crc_crlno = '" & frmComm.txtCRLNo.Text & "'"
                com.Connection = conn
                dr = com.ExecuteReader
                dr.Read()

                txtCRCNo.Text = dr!crc_no
                txtCLTAcctNo.Text = dr!crc_clientno
                lblrootpayee.Text = dr!crc_rootpayee
                txtPayeeName.Text = dr!crc_payeename
                txtClientName.Text = dr!crc_clientname
                txtProjName.Text = dr!crc_projectname
                txtCommType.Text = dr!crc_commtype
                txtGrossAmt.Text = dr!crc_grossamount
                txtWTax.Text = dr!crc_withheld
                txtCALType1.Text = dr!crc_caliqtype1
                txtCALDate1.Text = dr!crc_caliqdate1
                txtCALNo1.Text = dr!crc_caliqnum1
                txtCALAmt1.Text = dr!crc_caliqamt1
                txtCALDesc1.Text = dr!crc_caliqdesc1
                txtCALType2.Text = dr!crc_caliqtype2
                txtCALNo2.Text = dr!crc_caliqnum2
                txtCALAmt2.Text = dr!crc_caliqnum2
                txtCALDate2.Text = dr!crc_caliqdate2
                txtCALDesc2.Text = dr!crc_caliqdesc2
                txtCALType3.Text = dr!crc_caliqtype3
                txtCALDate3.Text = dr!crc_caliqdate3
                txtCALNo3.Text = dr!crc_caliqnum3
                txtCALAmt3.Text = dr!crc_caliqamt3
                txtCALDesc3.Text = dr!crc_caliqdesc3
                txtCALIQ.Text = dr!crc_caliqtotal
                txtNetAmt.Text = dr!crc_netamount
                txtVoucherNum.Text = dr!crc_voucherno
                txtCRLNum.Text = dr!crc_crlno
                txtDate.Text = dr!crc_date
                txtCPFNo.Text = dr!crc_cpfno
                txtPreparedBy.Text = dr!crc_preparedby
                txtCheckedBy.Text = dr!crc_checkedby
                txtApprovedBy.Text = dr!crc_approvedby
                txtGrossPrice.Text = dr!crc_grossprice
                txtLess1.Text = dr!crc_lessprice1
                txtDesc1.Text = dr!crc_lessdesc1
                txtLess2.Text = dr!crc_lessprice2
                txtDesc2.Text = dr!crc_lessdesc2
                txtLess3.Text = dr!crc_lessprice3
                txtDesc3.Text = dr!crc_lessdesc3
                txtNetSellingP.Text = dr!crc_netsellingprice
                txtRate.Text = dr!crc_rate
                txtGrossRel.Text = dr!crc_grossrelease
                txtDed1.Text = dr!crc_ded1
                txtDDesc1.Text = dr!crc_deddesc1
                txtDed2.Text = dr!crc_ded2
                txtDDesc2.Text = dr!crc_deddesc2
                txtDed3.Text = dr!crc_ded3
                txtDDesc3.Text = dr!crc_deddesc3
                txtDed4.Text = dr!crc_ded4
                txtDDesc4.Text = dr!crc_deddesc4
                txtDed5.Text = dr!crc_ded5
                txtNetRel.Text = dr!crc_netrelease
                txtCollectibles.Text = dr!crc_collectibles


                conn.Close()

            End If

End Sub

It doesn't seem to work. Whenever the form opens, even though txtCRCNum1 has a value, it will display the default values, and does not seem to execute the query. Can you please tell me where I got it wrong? Because I've been trying all sorts of solution I can find online and think of but nothing seems to work on this. Please help me.

Just for kicks.
Try putting that code in a separate method and call it from the Load method.
But also include a class boolean variable.

Private bIsLoading As Boolean = True

Private Sub frmcrc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If bIsLoading Then
        LoadValues()
    End If
    bIsLoading = False
End Sub

Private Sub LoadValues()
    'Your code
End Sub

This is just to make sure that the code is not being executed twice.
I've had the happen to me once or twice when I had some code in the Form_Load event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.