VBA - IF statements with AND not working - vba

Any help would be great. I cant seem to get the ELSEIF with the AND to work.
I am using a user-form. When they press the button the user form appears with check boxes. I am having trouble when both check boxes are checked. individually they work fine,
I am not sure what i am doing wrong. any help would be greatly appreciated
If Intact.Value = True Then
storDate = Sheets("escalation").Cells(Selection.Row, 2)
storProject = Sheets("escalation").Cells(Selection.Row, 3)
StorBill = Sheets("escalation").Cells(Selection.Row, 4)
storIntact = "Intact"
With objWord.ActiveDocument
.formfields("text2").Result = storDate
.formfields("Text3").Result = storProject
.formfields("Text4").Result = StorBill
.formfields("Text9").Result = storIntact
End With
ElseIf Compugen.Value = True Then
storDate = Sheets("escalation").Cells(Selection.Row, 2)
storProject = Sheets("escalation").Cells(Selection.Row, 3)
StorBill = Sheets("escalation").Cells(Selection.Row, 4)
storCompugen = "Compugen"
With objWord.ActiveDocument
.formfields("text2").Result = storDate
.formfields("Text3").Result = storProject
.formfields("Text4").Result = StorBill
.formfields("Text9").Result = storCompugen
End With
ElseIf Intact.Value And Compugen.Value = True Then
storDate = Sheets("escalation").Cells(Selection.Row, 2)
storProject = Sheets("escalation").Cells(Selection.Row, 3)
StorBill = Sheets("escalation").Cells(Selection.Row, 4)
storIntact = "Intact"
storDate1 = Sheets("escalation").Cells(Selection.Row, 2)
storProject1 = Sheets("escalation").Cells(Selection.Row, 3)
StorBill1 = Sheets("escalation").Cells(Selection.Row, 4)
storCompugen = "Compugen"
With objWord.ActiveDocument
.formfields("text2").Result = storDate
.formfields("Text3").Result = storProject
.formfields("Text4").Result = StorBill
.formfields("Text9").Result = storIntact
.formfields("text5").Result = storDate1
.formfields("Text6").Result = storProject1
.formfields("Text7").Result = StorBill1
.formfields("Text8").Result = storCompugen
End With
End If
Thanks in advance

Try changing the order. Otherwise as soon as one condition is met the If clause is exited.
If Intact.Value And Compugen.Value Then
'code
ElseIf Intact.Value Then
'code
ElseIf Compugen.Value Then
'code
End If

If Intact.Value = True is true then the first, not the third block will run.
Similarly if Intact.Value = True is not true and Compugen.Value = True is true, then the second block will run.
So you can see that the third block is not reachable.
The solution is to put the Intact.Value = True And Compugen.Value = True case first in the group.
Finally, Foo.Value = True is a tautology of the simpler Foo.Value. You can drop all the explicit = True comparisons.

Related

How to determine what is causing catastrophic error

I am getting catastrophic error at certain sequence on listbox form selection...
how can I determine what exactly is causing catastrophic error? The error is repeatable from my file.
On bellow code I put msgbox "a" and error doesn't appear any more... If I remove that it will apear again. Also it appears only right after I run the excel file and open forms and select item on that listbox.
Also the code is around 4000 lines and 150000 characters for this specific form. With other classes it's over 10000 lines and over 300000 characters.
Private Sub ListBox3_Change()
Dim partf As Variant, ctype As String
ReDim fncleft(4)
ReDim fncright(4)
Dim i As Integer, h As Integer
For i = 0 To Me.ListBox3.ListCount - 1
If Me.ListBox3.Selected(i) = True Then
With bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings
If Me.ListBox3.List(i, 1) <> vbNullString Then
Me.CheckBox_SQLltrim.Enabled = True
Me.CheckBox_SQLrtrim.Enabled = True
Me.CheckBox_SQLtrim.Enabled = True
Me.CheckBox_SQLlower.Enabled = True
Me.CheckBox_SQLupper.Enabled = True
Me.CommandButtonSQLreplace.Enabled = True
Me.Frame9.Visible = True
Me.TextBox_ConstraintType.Visible = True
Me.CommandButton8.Visible = True
Me.Label99.Visible = True
Me.Label100.Visible = True
Me.TextBox_ConstrTableName.Visible = True
Me.TextBox_ConstrColumnName.Visible = True
If .TableExists(Me.ListBox3.List(i, 0)) = True Then
If .Table(Me.ListBox3.List(i, 0)).ColumnExists(Me.ListBox3.List(i, 1)) = True Then
Me.ComboBox_PartTable.List = .Tables
Me.ComboBox_PartTable.AddItem vbNullString, 0
With .Table(Me.ListBox3.List(i, 0))
Me.CheckBox_FullJoinTable.Enabled = True
Me.CheckBox_FullJoinTable.Value = .FullJoin
With .column(Me.ListBox3.List(i, 1))
Me.TextBox_ExportName.Value = .Value
Me.CheckBox_ColumnHidden.Value = .Hidden
Me.CheckBox_ColumnEnabled.Value = .Active
Me.TextBox_ConstraintType.Value = .ConstraintType
Me.TextBox_ConstrTableName.Value = .ConstraintTable
Me.TextBox_ConstrColumnName.Value = .ConstraintColumn
Me.CheckBox_SQLltrim.Value = .SQLltrim
Me.CheckBox_SQLrtrim.Value = .SQLrtrim
Me.CheckBox_SQLtrim.Value = .SQLtrim
Me.CheckBox_SQLlower.Value = .SQLlower
Me.CheckBox_SQLupper.Value = .SQLupper
MsgBox "b"
partf = .PartitionFilter
If Not UBound(partf) < 0 Then
Me.TextBox_PartResults.Value = partf(0)
If bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings.TableExists(partf(1)) = True Then
Me.ComboBox_PartTable.Value = partf(1)
Me.ComboBox_PartColumn.List = bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings.Table(Me.ComboBox_PartTable.Value).Columns
Me.ComboBox_PartColumn.AddItem vbNullString, 0
If bomfix.ExcelTables(Me.ComboBox_Tables2.Value).settings.Table(partf(1)).ColumnExists(partf(2)) = True Then
Me.ComboBox_PartColumn.Value = partf(2)
Me.ComboBox_PartOrderBy.Value = partf(3)
End If
End If
Else
Me.TextBox_PartResults.Value = vbNullString
Me.ComboBox_PartTable.Value = vbNullString
Me.ComboBox_PartColumn.Value = vbNullString
Me.ComboBox_PartOrderBy.Value = vbNullString
End If
Me.TextBox_FuncLeftCol.Value = Me.ListBox3.List(i, 0) & "." & Me.ListBox3.List(i, 1)
Me.ComboBox_FuncRightVal.Value = Me.ComboBox_FuncRightVal.List(0)
' add filters Array(and_or, Comparator, tableright, columnright, FunctionRight, FunctionLeft)
Me.ListBox4.Clear
For h = 1 To .FilterSet.Count
Me.ListBox4.AddItem
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = .FilterSet(h)(0) ' and_or
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Join(.FilterSet(h)(5), "|") ' function left
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = .FilterSet(h)(1) ' comparator
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = .FilterSet(h)(2) ' table right
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = .FilterSet(h)(3) ' column right
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = Join(.FilterSet(h)(4), "|") ' function right
Next
End With
End With
End If
End If
ElseIf Me.ListBox3.List(i, 2) <> vbNullString Then
If .Aggregates.Exists(Me.ListBox3.List(i, 0)) = True Then
With .Aggregates(Me.ListBox3.List(i, 0))(1)
Me.CheckBox_SQLltrim.Enabled = False
Me.CheckBox_SQLrtrim.Enabled = False
Me.CheckBox_SQLtrim.Enabled = False
Me.CheckBox_SQLlower.Enabled = False
Me.CheckBox_SQLupper.Enabled = False
Me.CommandButtonSQLreplace.Enabled = False
Me.CheckBox_FullJoinTable.Enabled = False
Me.Frame9.Visible = False
Me.TextBox_ConstraintType.Visible = False
Me.CommandButton8.Visible = False
Me.Label99.Visible = False
Me.Label100.Visible = False
Me.TextBox_ConstrTableName.Visible = False
Me.TextBox_ConstrColumnName.Visible = False
Me.TextBox_ExportName.Value = .Value
Me.CheckBox_ColumnHidden.Value = .Hidden
Me.CheckBox_ColumnEnabled.Value = .Active
Me.TextBox_ConstraintType.Value = .ConstraintType
Me.TextBox_ConstrTableName.Value = .ConstraintTable
Me.TextBox_ConstrColumnName.Value = .ConstraintColumn
Me.TextBox_FuncLeftCol.Value = "aggregate"
Me.ComboBox_FuncRightVal.Value = Me.ComboBox_FuncRightVal.List(0)
' add filters Array(and_or, Comparator, tableright, columnright, FunctionRight, FunctionLeft)
Me.ListBox4.Clear
For h = 1 To .FilterSet.Count
Me.ListBox4.AddItem
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 0) = .FilterSet(h)(0)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 1) = Join(.FilterSet(h)(5), "|")
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 2) = .FilterSet(h)(1)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 3) = .FilterSet(h)(2)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 4) = .FilterSet(h)(3)
Me.ListBox4.List(Me.ListBox4.ListCount - 1, 5) = Join(.FilterSet(h)(4), "|")
Next
End With
End If
End If
End With
Exit For
End If
Next
Me.ComboBox_FuncTypeRight.Value = Me.ComboBox_FuncTypeRight.List(0)
Me.ComboBox_FuncTypeLeft.Value = Me.ComboBox_FuncTypeLeft.List(0)
Call ComboBox_FuncComp_Change
End Sub

VB replacing an object in a list

I have 2 lists approvedSuppliers and originalSupplierData
When approved Suppliers gets populated we clone the entry into the originalSupplierData . If they have modified a record but don't save we ask the user if they want to revert the changes . If they want to revert I am trying to replace the entry in approved suppliers with a clone of the original data. My current code for the revert is
Public Sub RevertChanges(SupplierID As Integer)
Dim orignalSupplier As Approved_Supplier = originalSupplierlist.Where(Function(x) x.ID = SupplierID).Single()
Dim modifiedSupplier As Approved_Supplier = ApprovedSuppliers.Where(Function(x) x.ID = SupplierID).Single()
modifiedSupplier = orignalSupplier.Clone
End Sub
The modifiedSupplier gets updated with the original values however the actual item in the list is not updated with the values.
If I modify one of the properties the list gets update. I am not sure what i am doing wrong can anyone point me in the right direction please?
Edit
The code for populating the list from the database is
supplierTableAdapter.Fill(supplierTable)
_approvedSuppliers = New List(Of Approved_Supplier)
originalSupplierlist = New List(Of Approved_Supplier)()
For Each row As ApprovedSuppliersDataset.ApprovedSupplierRow In supplierTable
supplier = New Approved_Supplier()
supplier.supplierID = row.PLSupplierAccountID
supplier.AccountNumber = row.SupplierAccountNumber
supplier.SupplierName = row.SupplierAccountName
supplier.SupplierAddress = CompileAddress(row)
supplier.Phone = CompilePhoneNumber(row)
If row.IsIDNull = False Then
supplier.ID = row.ID
If row.IsAdded_ByNull = False Then
supplier.AddedBy = row.Added_By
End If
If row.IsApprovedNull = False Then
supplier.Approved = row.Approved
End If
If row.IsAuditorNull = False Then
supplier.Auditor = row.Auditor
End If
If row.IsAudit_CommentsNull = False Then
supplier.AuditComments = row.Audit_Comments
End If
If row.IsAudit_DateNull = False Then
supplier.AuditDate = row.Audit_Date
End If
If row.IsDate_AddedNull = False Then
supplier.DateAdded = row.Date_Added
End If
If row.IsNotesNull = False Then
supplier.Notes = row.Notes
End If
If row.IsQuestionnaire_Return_DateNull = False Then
supplier.QuestionnaireReturnDate = row.Questionnaire_Return_Date
End If
If row.IsQuestionnaire_Sent_DateNull = False Then
supplier.QuestionnaireSentDate = row.Questionnaire_Sent_Date
End If
If row.IsQuestionnaire_StatusNull = False Then
supplier.QuestionnaireStatus = row.Questionnaire_Status
End If
If row.IsReplinNull = False Then
supplier.Replin = row.Replin
End If
If row.IsReview_CommentsNull = False Then
supplier.ReviewComment = row.Review_Comments
End If
If row.IsReview_DateNull = False Then
supplier.ReviewDate = row.Review_Date
End If
If row.IsReviewerNull = False Then
supplier.Reviewers = row.Reviewer
End If
If row.IsStakeholder_ContactNull = False Then
supplier.StakeholderContact = row.Stakeholder_Contact
End If
If row.IsStandardsNull = False Then
supplier.Standards = row.Standards
End If
If row.IsStandard_ExpiryNull = False Then
supplier.StandardExpiry = row.Standard_Expiry
End If
If row.IsStatusNull = False Then
supplier.Status = row.Status
End If
If row.IsSupplier_Expiry_DateNull = False Then
supplier.SupplierExpiryDate = row.Supplier_Expiry_Date
End If
If row.IsSupplier_ScopeNull = False Then
supplier.SupplierScope = row.Supplier_Scope
End If
If row.Is_T_CsNull = False Then
supplier.TC = row._T_Cs
End If
End If
supplier.ClearISDirty()
_approvedSuppliers.Add(supplier)
originalSupplierlist.Add(supplier.Clone)
Next
and for the clone we have
Public Function Clone() As Object Implements ICloneable.Clone
Dim cloned As New Approved_Supplier()
cloned.ID = Me.ID
cloned.DateAdded = Me.DateAdded
cloned.Status = Me.Status
cloned.AddedBy = Me.AddedBy
cloned.Approved = Me.Approved
cloned.AuditDate = Me.AuditDate
cloned.Auditor = Me.Auditor
cloned.AuditComments = Me.AuditComments
cloned.QuestionnaireStatus = Me.QuestionnaireStatus
cloned.QuestionnaireSentDate = Me.QuestionnaireSentDate
cloned.QuestionnaireReturnDate = Me.QuestionnaireReturnDate
cloned.ReviewDate = Me.ReviewDate
cloned.Reviewers = Me.Reviewers
cloned.ReviewComment = Me.ReviewComment
cloned.Standards = Me.Standards
cloned.StandardExpiry = Me.StandardExpiry
cloned.SupplierScope = Me.SupplierScope
cloned.Replin = Me.Replin
cloned.TC = Me.TC
cloned.Notes = Me.Notes
cloned.StakeholderContact = Me.StakeholderContact
cloned.SupplierExpiryDate = Me.SupplierExpiryDate
cloned.supplierID = Me.supplierID
cloned.AccountNumber = Me.AccountNumber
cloned.SupplierName = Me.SupplierName
cloned.SupplierAddress = Me.SupplierAddress
cloned.Phone = Me.Phone
cloned.Email = Me.Email
cloned.ClearISDirty()
Return cloned
End Function
You are not replacing in the list by affecting modifiedSupplier.
Try by getting the index of the modifiedSupplier and then replacing the item at the found index by your clone.
Public Sub RevertChanges(SupplierID As Integer)
Dim orignalSupplier As Approved_Supplier = originalSupplierlist.Where(Function(x) x.ID = SupplierID).Single()
Dim modifiedIndex As Integer = ApprovedSuppliers.FindIndex(Function(x) x.ID = SupplierID)
ApprovedSuppliers(modifiedIndex) = orignalSupplier.Clone()
End Sub

Dev Express chart Not Updating Second Time in Vb.net

For the first time when I run my window application it will successfully run, but when I change the database column and binding then it gives me an error that specific data column is not there in database; also I am make null chart datasource but it gives an error. Please help.
Dim ctrArr As Integer
Dim serCnt As Integer
Dim series1 As New Series
Dim seriesFound As Boolean = False
For serCnt = 0 To ctrChart.Series.Count - 1
ctrChart.Series(0).ArgumentDataMember = ""
ctrChart.Series(0).ValueDataMembers(0) = ""
ctrChart.Series(0).Visible = False
Next
For ctrArr = 0 To gStrYAxisParamArray.Length - 1 'deptname'
For serCnt = 0 To ctrChart.Series.Count - 1
If UCase(Trim(gStrYAxisParamArray(ctrArr))) = UCase(ctrChart.Series(serCnt).Name.ToString) Then
ctrChart.Series(serCnt).ArgumentDataMember = ""
ctrChart.Series(serCnt).ValueDataMembers(0) = ""
ctrChart.Series(serCnt).Visible = True
ctrChart.Series(serCnt).ArgumentDataMember = gxAxis
ctrChart.Series(serCnt).ValueDataMembers.Item(0) = Trim(gStrYAxisParamArray(ctrArr))
seriesFound = True
Exit For
End If
Next
If seriesFound = False Then
series1 = New Series(Trim(gStrYAxisParamArray(ctrArr)).ToString, ViewType.Bar)
'ctrChart.Series.AddRange(New Series() {series1, series2})
ctrChart.Series.Add(series1)
series1.ArgumentDataMember = ""
series1.ValueDataMembers(0) = ""
series1.Visible = True
series1.ArgumentDataMember = gxAxis
series1.Label.Border.Visible = False
series1.ValueDataMembers(0) = Trim(gStrYAxisParamArray(ctrArr))
series1.LegendText = Trim(gStrYAxisParamArray(ctrArr).ToString)
End If
seriesFound = False
Next
cmbSeries.Items.Clear()
For ctrArr = 0 To ChrtStockDept.Series.Count - 1
With cmbSeries.Items
cmbSeries.Items.Add(ChrtStockDept.Series(ctrArr).Name.ToString)
End With
Next

Error code "Can't finde project or library" in vba

I'm making a vba project with some userforms. I want the people to click the macro button, then need to choose their initials, then by togglebutton click the weeks they want to go on holiday and then click transfer. So far i've done, that if they click one their initials, the value will be printed to a cell. The next userform (userform2), will need to look for the value, to determin wich row the values will be printed in.
But i get an error, on my Select case code.
Private Sub CommandButton1_Click()
Dim score As Integer, result As String
score = ActiveSheet.Range("A19").Value
Select Case score
Case Is = "CTK"
result = Something
Case Is = "MogM"
result = Something1
Case Is = "KSJI"
result = Something2
Case Is = "TSLR"
result = Something3
Case Is = "JOHQ"
result = Something4
Case Is = "OLGJ"
result = Something5
Case Is = "CBJN"
result = Something6
Case Is = "JVLK"
result = Something7
Case Is = "JFP"
result = Something8
Case Is = "EVAD"
result = Something9
Case Is = "TKUP"
result = Something10
Case Else
MsgBox ("Didn't find anyone with these init")
Range("A20").Value = result
End Select
End Sub
Private Sub ToggleButton1_Click()
End Sub
I solved the coding, but now i got a new question. Is it possible to simplify it?
Private Sub CommandButton1_Click()
Dim score As Integer, result As String
score = ActiveSheet.Range("A19").Value
Select Case score
Case Is = "1"
result = "CTK"
Case Is = "2"
result = "MogM"
Case Is = "3"
result = "KSJI"
Case Is = "4"
result = "TSLR"
Case Is = "5"
result = "JOHQ"
Case Is = "6"
result = "OLGJ"
Case Is = "7"
result = "CBJN"
Case Is = "8"
result = "JVLK"
Case Is = "9"
result = "JFP"
Case Is = "10"
result = "EVAD"
Case Is = "11"
result = "TKUP"
Case Else
MsgBox ("Didn't find anyone with these init")
End Select
Range("A20").Value = result
'CTK opportunities
If result = "CTK" And ToggleButton1 = True Then Range("B4").Value = "X"
If result = "CTK" And ToggleButton2 = True Then Range("C4").Value = "X"
If result = "CTK" And ToggleButton3 = True Then Range("D4").Value = "X"
If result = "CTK" And ToggleButton4 = True Then Range("E4").Value = "X"
If result = "CTK" And ToggleButton5 = True Then Range("F4").Value = "X"
If result = "CTK" And ToggleButton6 = True Then Range("G4").Value = "X"
If result = "CTK" And ToggleButton7 = True Then Range("H4").Value = "X"
If result = "CTK" And ToggleButton8 = True Then Range("I4").Value = "X"
If result = "CTK" And ToggleButton9 = True Then Range("J4").Value = "X"
If result = "CTK" And ToggleButton10 = True Then Range("K4").Value = "X"
If result = "CTK" And ToggleButton11 = True Then Range("L4").Value = "X"
If result = "CTK" And ToggleButton12 = True Then Range("M4").Value = "X"
'Next person
Unload Me
End Sub

ScriptManager.RegisterClientScriptBlock hidden controls at runtime

I need your help. I would like to understand why when running ScriptManager.RegisterClientScriptBlock the controls of my page disappear and reappear only after confirmation of Ok?
Protected Sub ddlDeckFittingCategory_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlDeckFittingCategory.SelectedIndexChanged
If txbNumberofColumns.Text = "" Or Me.txbShellDiameter.Text = "" Then
ScriptManager.RegisterClientScriptBlock(Me.Page, Page.GetType, "alert", "alert('Informe o valor do Diâmetro do Casco (m)!');", True)
ddlDeckFittingCategory.SelectedValue = -1
Else
If Request("TipoTela") = 1 Then
If ddlDeckFittingCategory.SelectedValue = "Typical" Then
objFinttings_temp.IncluirFittingsTempTQIFLTTipico(Session("cod_usuario_usu"))
'objFinttings_temp.AtualizaFittingsTempColumnWell_24_in_Diam(CType(txbNumberofColumns.Text, Double))
objFinttings_temp.AtualizaFittingsTempColumnWell_24_in_Diam(txbNumberofColumns.Text)
tbFittingsFonte.Visible = True
tbFittingsFonte.HeaderText = ""
TcPrincipal.ActiveTabIndex = 6
Dim dvConsultarCodFonteEmFittingsTempPorUsuario As DataView = objFinttings_temp.ConsultarCodFonteEmFittingsTempPorUsuario(Session("cod_usuario_usu"))
Session("cod_fonte_fon") = dvConsultarCodFonteEmFittingsTempPorUsuario.Table.Rows(0)("cod_fonte_fon")
Session("ddlDeckFittingCategory") = ddlDeckFittingCategory.SelectedValue
Else
objFinttings_temp.IncluirFittingsTQIFLTDetalhado(0)
tbFittings.Visible = True
tbFittings.HeaderText = ""
TcPrincipal.ActiveTabIndex = 6
End If
GrvFittingsFonte.DataBind()
Else
If ddlDeckFittingCategory.SelectedValue = "Typical" Then
objFinttings_temp.IncluirFittingsTempTQIFLTTipico(Session("cod_usuario_usu"))
'objFinttings_temp.AtualizaFittingsTempColumnWell_24_in_Diam(CType(txbNumberofColumns.Text, Double))
objFinttings_temp.AtualizaFittingsTempColumnWell_24_in_Diam(txbNumberofColumns.Text)
tbFittingsFonte.Visible = True
tbFittingsFonte.HeaderText = ""
TcPrincipal.ActiveTabIndex = 6
Else
objFinttings_temp.IncluirFittingsTQIFLTDetalhado(Session("cod_fonte_fon"))
tbFittings.Visible = True
tbFittings.HeaderText = ""
TcPrincipal.ActiveTabIndex = 6
End If
GrvFittingsFonte.DataBind()
If ddlSelfSupportingRoof.SelectedValue = 1 Or ddlSelfSupportingRoof.SelectedValue = "-1" Then
txbNumberofColumns.Enabled = False
rvNumColuna.Visible = False
ddlEffectiveColumnDiameter.Enabled = False
rvDiametroEfetivoColuna.Visible = False
Else
txbNumberofColumns.Enabled = True
rvNumColuna.Visible = True
ddlEffectiveColumnDiameter.Enabled = True
rvDiametroEfetivoColuna.Visible = True
End If
End If
End If
End Sub
enter code here
use Page.ClientScript.RegisterStartupScript()
it will run after the page loads.