How to speed up "inner join" query in access? - sql

I'm working on an access project using vba and one of the programs has several queries like UPDATE A INNER JOIN B ON condition SET field = value .Both table A and Table B have hundreds of thousands records.
The annoying thing is these queries will take more than ten hours to execute and I was trying to use index to improve the performance but failed. Any suggestions to help me out?
Here is one of the queries:
strSQL = "UPDATE [csd_dis_" & i_year & "_all] as d INNER JOIN [adm_" & a_yyyy & "] as aa ON d.d_cino = aa.a_cino and cstr(d.d_sentdte) = cstr(aa.a_sentdte) and right('00' + trim(str(d.m_d_nature)),2) = left(trim(cstr(aa.m_a_nature)),2) "
strSQL = strSQL & "SET d.match_adm = " & kk & ",d.a_local_p = aa.a_local_p,d.inst = aa.c_inst, d.dosy = aa.dosy,d.dosm = aa.dosm,d.dosd = aa.dosd, " _
& "d.a_sentdte = aa.a_sentdte,d.tid = aa.tid,d.crb = aa.crb,d.crbchk = aa.crbchk, d.court = aa.court,d.ccn = aa.ccn,d.cyr = aa.cyr,d.a_nature = aa.nature,d.nature_o = aa.nature_o, " _
& "d.m_a_nature = aa.m_a_nature,d.class = aa.class,d.cat = aa.cat,d.ilossp = aa.ilossp,d.chap = aa.chap,d.sect = aa.sect,d.newoff = aa.newoff,d.hkid = aa.hkid,d.sex = aa.sex,d.doby = aa.doby,d.dobm = aa.dobm, " _
& "d.dobd = aa.dobd,d.a_dob = aa.a_dob,d.a_dob_imp = aa.a_dob_imp,d.age = aa.age,d.accom = aa.accom,d.disres = aa.disres,d.yrres = aa.yrres,d.edu = aa.edu,d.marst = aa.marst,d.bplbir = aa.bplbir,d.plbir = aa.plbir,d.yrhk = aa.yrhk, " _
& "d.[on] = aa.[on],d.sently = aa.sently,d.sentlm = aa.sentlm,d.sentld = aa.sentld,d.tr = aa.tr,d.dr = aa.dr,d.mr = aa.mr,d.inr = aa.inr,d.pvdg = aa.pvdg, d.pvndg = aa.pvndg,d.pvcn = aa.pvcn,d.pvcn2 = aa.pvcn2,d.bghome = aa.bghome, " _
& "d.pvdc = aa.pvdc,d.pvtc = aa.pvtc,d.pvdatc = aa.pvdatc,d.pvrc = aa.pvrc,d.pvpri = aa.pvpri,d.pvinst = aa.pvinst,d.foffen = aa.foffen,d.dfcy = aa.dfcy,d.dfcm = aa.dfcm,d.dfcd = aa.dfcd,d.a_dfcdte = aa.a_dfcdte, " _
& "d.agefcn = aa.agefcn,d.trisoc = aa.trisoc,d.ddpur = aa.ddpur,d.dcost = aa.dcost,d.cumd = aa.cumd,d.needle = aa.needle,d.yrdg = aa.yrdg,d.causa = aa.causa,d.fdg = aa.fdg,d.fmd = aa.fmd,d.agefdg = aa.agefdg, " _
& "d.yradd = aa.yradd,d.asso = aa.asso,d.la = aa.la,d.origin = aa.origin,d.mode = aa.mode,d.arrest = aa.arrest,d.stayy = aa.stayy,d.staym = aa.staym,d.stayd = aa.stayd,d.occ = aa.occ,d.name = aa.name,d.cname = aa.cname," _
& "d.othdocno = aa.othdocno,d.cudg1 = aa.cudg1,d.cudg2 = aa.cudg2,d.cudg3 = aa.cudg3,d.cudg4 = aa.cudg4,d.psydrug1 = aa.psydrug1,d.psydrug2 = aa.psydrug2,d.psydrug3 = aa.psydrug3, " _
& "d.psydrug4 = aa.psydrug4,d.agefpsy = aa.agefpsy,d.psydgfreq = aa.psydgfreq,d.psycausa = aa.psycausa WHERE '" & matchkey & "' and d.match_adm=' '"

Related

Linq with multiple where clauses not pulling back data

I am using VB.NET and Linq; I have two separate conditions that have to be satisfied. When I run them individually they work fine however when I combine them into a single statement with multiple where statements then I get 0 records returned.
If I run the first where clause without the 2nd I get 70 records
If I run the 2nd where clause without the first i get 5 records
When I run with both where clauses I get 0 records returned
Here is the code.
submissionDetails = (From r In model.NIBRS_ReportStatusByORI
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And Not r.ReportType = "ZERO REPORT" _
And r.OccurrenceDate >= sCritera.BeginDate _
And r.OccurrenceDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And r.ReportType.Equals("ZERO REPORT") _
And r.YearMonthDate >= sCritera.BeginDate _
And r.YearMonthDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Select New ReportStatusDetails() With {
.ChangeDate = r.Insertdate,
.IncidentId = r.IncidentID,
.IncidentIdentifier = r.IncidentIdentifier,
.OriginatingORI = r.OriginatingORI,
.ReportType = r.ReportType,
.StatusID = r.StatusID,
.SubmittedBy = r.Username,
.ReportDate = r.YearMonthDate,
.ReportDateString = r.YearMonth,
.OccurrenceDate = r.OccurrenceDate
}).ToList()
Have you tried this:
submissionDetails = (From r In model.NIBRS_ReportStatusByORI
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And Not r.ReportType = "ZERO REPORT" _
And r.OccurrenceDate >= sCritera.BeginDate _
And r.OccurrenceDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Select New ReportStatusDetails() With {
.ChangeDate = r.Insertdate,
.IncidentId = r.IncidentID,
.IncidentIdentifier = r.IncidentIdentifier,
.OriginatingORI = r.OriginatingORI,
.ReportType = r.ReportType,
.StatusID = r.StatusID,
.SubmittedBy = r.Username,
.ReportDate = r.YearMonthDate,
.ReportDateString = r.YearMonth,
.OccurrenceDate = r.OccurrenceDate
}).Union(From r In model.NIBRS_ReportStatusByORI
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And r.ReportType.Equals("ZERO REPORT") _
And r.YearMonthDate >= sCritera.BeginDate _
And r.YearMonthDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Select New ReportStatusDetails() With {
.ChangeDate = r.Insertdate,
.IncidentId = r.IncidentID,
.IncidentIdentifier = r.IncidentIdentifier,
.OriginatingORI = r.OriginatingORI,
.ReportType = r.ReportType,
.StatusID = r.StatusID,
.SubmittedBy = r.Username,
.ReportDate = r.YearMonthDate,
.ReportDateString = r.YearMonth,
.OccurrenceDate = r.OccurrenceDate
}).ToList()

'System.ArgumentOutOfRangeException'"Tried the other solution but never gotten to the point of resolving" [duplicate]

This question already has answers here:
What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
(5 answers)
Closed 2 years ago.
i've tried the other solution(like changing item that should be shown on data) but i think i've never gotten the point to resolving. Thank you in advance whoever can answer my problem...
Private Sub dgEmp_Click(sender As Object, e As EventArgs) Handles dgEmp.Click
LoadEmployeeInfo(dgEmp.SelectedRows.Item(0).Index)
End Sub
Private Sub LoadEmployee(Optional q As String = "")
list.Query = "Select id,lastname,firstname,middlename,sss,philh,pag,rate,cola,mStatus,free_insurance,mp,mpvalue from tblemployee where (lastname like'%" & q & "%' or firstname like'%" & q & "%' or middlename like'%" & q & "%') and deactive='No' order by lastname,firstname,middlename"
list.datagrid = dgEmp
list.LoadRecords()
If list.RecordCount = Nothing Then Exit Sub
LoadEmployeeInfo(dgEmp.SelectedRows.Item(0).Index)
End Sub
Public Sub LoadEmployeeInfo(index As Integer)
With dgEmp.Rows(index)
id = .Cells(0).Value
lblName.Text = .Cells(1).Value & ", " & .Cells(2).Value & " " & .Cells(3).Value
rpd = .Cells(7).Value
lblRate.Text = Format(rpd, "#,##0.000000000")
cola = .Cells(8).Value
lblAllo.Text = Format(cola, "#,##0.000000000")
otrate = (rpd / 8) * 1.25
lblOTRate.Text = Format(otrate, "#,##0.000000000")
IsSSS = ConvertToBoolean(.Cells(4).Value)
IsPH = ConvertToBoolean(.Cells(5).Value) 'add
IsPAG = ConvertToBoolean(.Cells(6).Value) 'pos
IsMP = ConvertToBoolean(.Cells(11).Value)
IsFI = ConvertToBoolean(.Cells(10).Value)
CStatus = .Cells(9).Value
MPV = .Cells(12).Value
End With
ThisPayroll.Query = "Select * from tblpayroll where payrollperiod=? and empid=?"
ThisPayroll.AddParam("#payrollperiod", GetPeriod)
ThisPayroll.AddParam("#empid", id)
ThisPayroll.ExecQuery()
If ThisPayroll.RecordCount = Nothing Then
isUpdate = False
txtReg_Days.Text = 0
txtReg_OT.Text = 0
txtSP_Days.Text = 0
txtSP_OT.Text = 0
txtHoliday.Text = 0
txtHolidayOT.Text = 0
txtLate.Text = 0
txtAdjustment.Text = 0
txtSSSL.Text = 0
txtHDMFL.Text = 0
txtCA.Text = 0
txtDMA.Text = 0
txtRice.Text = 0
txtCloth.Text = 0
txtEmpMed.Text = 0
txtLaundry.Text = 0
txtMeal.Text = 0
Else
With ThisPayroll.DataSource
isUpdate = True
txtReg_Days.Text = .Rows(0)("regday")
txtReg_OT.Text = .Rows(0)("ot")
txtSP_Days.Text = .Rows(0)("spday")
txtSP_OT.Text = .Rows(0)("spdayot")
txtHoliday.Text = .Rows(0)("lholiday")
txtHolidayOT.Text = .Rows(0)("lhot")
txtLate.Text = .Rows(0)("hlate")
txtAdjustment.Text = .Rows(0)("salary_adj")
txtSSSL.Text = .Rows(0)("sss_loan")
txtHDMFL.Text = .Rows(0)("pag_loan")
txtCA.Text = .Rows(0)("cash_advance")
txtDMA.Text = .Rows(0)("depmed")
txtRice.Text = .Rows(0)("ricesub")
txtCloth.Text = .Rows(0)("clothing")
txtEmpMed.Text = .Rows(0)("empmed")
txtLaundry.Text = .Rows(0)("laundry")
txtMeal.Text = .Rows(0)("meal")
End With
End If
Compute()
End Sub
If you cannot make sure that something is selected elsewhere, you can last-minute check it like this:
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If dgEmp.SelectedRows.Count > 0 Then
LoadEmployeeInfo(dgEmp.SelectedRows.Item(0).Index)
End If
End Sub
It works only because you're using index zero, or else you would have to be careful for your index, too. Of course, this is assuming that dgEmp is not Nothing...
Also, notice that I attached this to the SelectionChanged event, as I don't think that the Click event will give you what you want, but I'll let that part for you to deal with. Have fun!

I am trying to check if a value isl Resistant in a table from a Form using dlookup

I am trying to lookup a value in a table for a particular patient ID and see whether that patient has the value Resistant. If so then disable a particular button on the form. I tried the following dlookup but it's giving me compiler error:
If DLookup("Rifampicin", "TableGeneXpert", "[PatientID] = " & Forms.FrmTreatment!PatientID) = Resistant Then
Me.btnDSTMatch.Enabled = True
Else
Me.btnDSTMatch.Enabled = False
Try with:
If DLookup("Rifampicin", "TableGeneXpert", "[PatientID] = " & Forms.FrmTreatment!PatientID & "") = "Resistant" Then
Me.btnDSTMatch.Enabled = True
Else
Me.btnDSTMatch.Enabled = False
End If
Or perhaps directly:
Me.btnDSTMatch.Enabled = IsNull(DLookup("Rifampicin", "TableGeneXpert", "[PatientID] = " & Forms.FrmTreatment!PatientID & " And [Rifampicin] = 'Resistant'"))
To filter on the latest date you can include a DMax expression or (the little known option) an SQL filter:
Me.btnDSTMatch.Enabled = IsNull(DLookup("Rifampicin", "TableGeneXpert", "[PatientID] = " & Forms.FrmTreatment!PatientID & " And [Rifampicin] = 'Resistant' And [JournalDate] = (Select Max([JournalDate]) From TableGeneXpert Where [PatientID] = " & Forms.FrmTreatment!PatientID & " And [Rifampicin] = 'Resistant')"))

Automation Error VBA from Userform

Keep getting an automation error when my userform uses the below code when it initializes. I dont get an error when i take it out. The userform is being called from a shape using a module. the text from the userform is stored under a sheet called "Compliance". My userform is also called compliance. Here is my code below, any help would be much appreciated:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Set ws = Sheets("compliance")
On Error Resume Next
With page1
employee1.Value = ws.Range("B2")
employee2.Value = ws.Range("B3")
employee3.Value = ws.Range("B4")
employee4.Value = ws.Range("B5")
employee5.Value = ws.Range("B5")
employee6.Value = ws.Range("B6")
'//// setting ops grade
grade1.Value = ws.Range("D2")
grade2.Value = ws.Range("D3")
grade3.Value = ws.Range("D4")
grade4.Value = ws.Range("D5")
grade5.Value = ws.Range("D6")
grade6.Value = ws.Range("D7")
'//// setting employee compliance recap
recap1.Value = ws.Range("F2")
recap2.Value = ws.Range("F3")
recap3.Value = ws.Range("F4")
recap4.Value = ws.Range("F5")
recap5.Value = ws.Range("F6")
recap6.Value = ws.Range("F7")
'////////////////////////////////////////// febraury
'//// setting employees interviewed
With page2
febemp1.Value = ws.Range("B14")
febemp2.Value = ws.Range("B15")
febemp3.Value = ws.Range("B16")
febemp4.Value = ws.Range("B17")
febemp5.Value = ws.Range("B18")
febemp6.Value = ws.Range("B19")
febgrd1.Value = ws.Range("D14")
febgrd2.Value = ws.Range("D15")
febgrd3.Value = ws.Range("D16")
febgrd4.Value = ws.Range("D17")
febgrd5.Value = ws.Range("D18")
febgrd6.Value = ws.Range("D19")
febrecap1.Value = ws.Range("F14")
febrecap2.Value = ws.Range("F15")
febrecap3.Value = ws.Range("F16")
febrecap4.Value = ws.Range("F17")
febrecap5.Value = ws.Range("F18")
febrecap6.Value = ws.Range("F19")
With page3
marchemp1.Value = ws.Range("B26")
marchemp2.Value = ws.Range("B27")
marchemp3.Value = ws.Range("B28")
marchemp4.Value = ws.Range("B29")
marchemp5.Value = ws.Range("B30")
marchemp6.Value = ws.Range("B31")
marchgrd1.Value = ws.Range("D26")
marchgrd2.Value = ws.Range("D27")
marchgrd3.Value = ws.Range("D28")
marchgrd4.Value = ws.Range("D29")
marchgrd5.Value = ws.Range("D30")
marchgrd6.Value = ws.Range("D31")
marchrecap1.Value = ws.Range("F26")
marchrecap2.Value = ws.Range("F27")
marchrecap3.Value = ws.Range("F28")
marchrecap4.Value = ws.Range("F29")
marchrecap5.Value = ws.Range("F30")
marchrecap6.Value = ws.Range("F31")
With page4
apremp1.Value = ws.Range("B38")
apremp2.Value = ws.Range("B39")
apremp3.Value = ws.Range("B40")
apremp4.Value = ws.Range("B41")
apremp5.Value = ws.Range("B42")
apremp6.Value = ws.Range("B43")
aprgrd1.Value = ws.Range("D38")
aprgrd2.Value = ws.Range("D39")
aprgrd3.Value = ws.Range("D40")
aprgrd4.Value = ws.Range("D41")
aprgrd5.Value = ws.Range("D42")
aprgrd6.Value = ws.Range("D43")
aprrecap1.Value = ws.Range("F38")
aprrecap2.Value = ws.Range("F39")
aprrecap3.Value = ws.Range("F40")
aprrecap4.Value = ws.Range("F41")
aprrecap5.Value = ws.Range("F42")
aprrecap6.Value = ws.Range("F43")
With page5
mayemp1.Value = ws.Range("B50")
mayemp2.Value = ws.Range("B51")
mayemp3.Value = ws.Range("B52")
mayemp4.Value = ws.Range("B53")
mayemp5.Value = ws.Range("B54")
mayemp6.Value = ws.Range("B55")
maygrd1.Value = ws.Range("D50")
maygrd2.Value = ws.Range("D51")
maygrd3.Value = ws.Range("D52")
maygrd4.Value = ws.Range("D53")
maygrd5.Value = ws.Range("D54")
maygrd6.Value = ws.Range("D55")
mayrecap1.Value = ws.Range("F50")
mayrecap2.Value = ws.Range("F51")
mayrecap3.Value = ws.Range("F52")
mayrecap4.Value = ws.Range("F53")
mayrecap5.Value = ws.Range("F54")
mayrecap6.Value = ws.Range("F55")
With page6
junemp1.Value = ws.Range("B62")
junemp2.Value = ws.Range("B63")
junemp3.Value = ws.Range("B64")
junemp4.Value = ws.Range("B65")
junemp5.Value = ws.Range("B66")
junemp6.Value = ws.Range("B67")
jungrd1.Value = ws.Range("D62")
jungrd2.Value = ws.Range("D63")
jungrd3.Value = ws.Range("D64")
jungrd4.Value = ws.Range("D65")
jungrd5.Value = ws.Range("D66")
jungrd6.Value = ws.Range("D67")
junrecap1.Value = ws.Range("F62")
junrecap2.Value = ws.Range("F63")
junrecap3.Value = ws.Range("F64")
junrecap4.Value = ws.Range("F65")
junrecap5.Value = ws.Range("F66")
junrecap6.Value = ws.Range("F67")
With page7
julemp1.Value = ws.Range("B74")
julemp2.Value = ws.Range("B75")
julemp3.Value = ws.Range("B76")
julemp4.Value = ws.Range("B77")
julemp5.Value = ws.Range("B78")
julemp6.Value = ws.Range("B79")
julgrd1.Value = ws.Range("D74")
julgrd2.Value = ws.Range("D75")
julgrd3.Value = ws.Range("D76")
julgrd4.Value = ws.Range("D77")
julgrd5.Value = ws.Range("D78")
julgrd6.Value = ws.Range("D79")
julrecap1.Value = ws.Range("F74")
julrecap2.Value = ws.Range("F75")
julrecap3.Value = ws.Range("F76")
julrecap4.Value = ws.Range("F77")
julrecap5.Value = ws.Range("F78")
julrecap6.Value = ws.Range("F79")
With page8
augemp1.Value = ws.Range("B86")
augemp2.Value = ws.Range("B87")
augemp3.Value = ws.Range("B88")
augemp4.Value = ws.Range("B89")
augemp5.Value = ws.Range("B90")
augemp6.Value = ws.Range("B91")
auggrd1.Value = ws.Range("D86")
auggrd2.Value = ws.Range("D87")
auggrd3.Value = ws.Range("D88")
auggrd4.Value = ws.Range("D89")
auggrd5.Value = ws.Range("D90")
auggrd6.Value = ws.Range("D91")
augrecap1.Value = ws.Range("F86")
augrecap2.Value = ws.Range("F87")
augrecap3.Value = ws.Range("F88")
augrecap4.Value = ws.Range("F89")
augrecap5.Value = ws.Range("F90")
augrecap6.Value = ws.Range("F91")
With page9
sepemp1.Value = ws.Range("B98")
sepemp2.Value = ws.Range("B99")
sepemp3.Value = ws.Range("B100")
sepemp4.Value = ws.Range("B101")
sepemp5.Value = ws.Range("B102")
sepemp6.Value = ws.Range("B103")
sepgrd1.Value = ws.Range("D98")
sepgrd2.Value = ws.Range("D99")
sepgrd3.Value = ws.Range("D100")
sepgrd4.Value = ws.Range("D101")
sepgrd5.Value = ws.Range("D102")
sepgrd6.Value = ws.Range("D103")
seprecap1.Value = ws.Range("F98")
seprecap2.Value = ws.Range("F99")
seprecap3.Value = ws.Range("F100")
seprecap4.Value = ws.Range("F101")
seprecap5.Value = ws.Range("F102")
seprecap6.Value = ws.Range("F103")
With page10
octemp1.Value = ws.Range("B110")
octemp2.Value = ws.Range("B111")
octemp3.Value = ws.Range("B112")
octemp4.Value = ws.Range("B113")
octemp5.Value = ws.Range("B114")
octemp6.Value = ws.Range("B115")
octgrd1.Value = ws.Range("D110")
octgrd2.Value = ws.Range("D111")
octgrd3.Value = ws.Range("D112")
octgrd4.Value = ws.Range("D113")
octgrd5.Value = ws.Range("D114")
octgrd6.Value = ws.Range("D115")
octrecap1.Value = ws.Range("F110")
octrecap2.Value = ws.Range("F111")
octrecap3.Value = ws.Range("F112")
octrecap4.Value = ws.Range("F113")
octrecap5.Value = ws.Range("F114")
octrecap6.Value = ws.Range("F115")
With page11
novemp1.Value = ws.Range("B122")
novemp2.Value = ws.Range("B123")
novemp3.Value = ws.Range("B124")
novemp4.Value = ws.Range("B125")
novemp5.Value = ws.Range("B126")
novemp6.Value = ws.Range("B127")
novgrd1.Value = ws.Range("D122")
novgrd2.Value = ws.Range("D123")
novgrd3.Value = ws.Range("D124")
novgrd4.Value = ws.Range("D125")
novgrd5.Value = ws.Range("D126")
novgrd6.Value = ws.Range("D127")
novrecap1.Value = ws.Range("F122")
novrecap2.Value = ws.Range("F123")
novrecap3.Value = ws.Range("F124")
novrecap4.Value = ws.Range("F125")
novrecap5.Value = ws.Range("F126")
novrecap6.Value = ws.Range("F127")
With page12
decemp1.Value = ws.Range("B134")
decemp2.Value = ws.Range("B135")
decemp3.Value = ws.Range("B136")
decemp4.Value = ws.Range("B137")
decemp5.Value = ws.Range("B138")
decemp6.Value = ws.Range("B139")
decgrd1.Value = ws.Range("D134")
decgrd2.Value = ws.Range("D135")
decgrd3.Value = ws.Range("D136")
decgrd4.Value = ws.Range("D137")
decgrd5.Value = ws.Range("D138")
decgrd6.Value = ws.Range("D139")
decrecap1.Value = ws.Range("F134")
decrecap2.Value = ws.Range("F135")
decrecap3.Value = ws.Range("F136")
decrecap4.Value = ws.Range("F137")
decrecap5.Value = ws.Range("F138")
decrecap6.Value = ws.Range("F139")
End With
End With
End With
End With
End With
End With
End With
End With
End With
End With
End With
End With
End Sub
Untested - too many controls to make...
You'd need to harmonize your control naming a bit, but something like this should work:
Private Sub UserForm_Initialize()
Dim ws As Worksheet, mNum As Long, months, i As Long, m
Dim c As Range
Set ws = Sheets("compliance")
Set c = ws.Range("B1")
months = Array("jan", "feb", "mar", "apr", "may", "jun", _
"jul", "aug", "sep", "oct", "nov", "dec")
For mNum = 1 To 12
m = months(m - 1)
With Me.MultiPage1.Pages("page" & mNum)
For i = 1 To 6
.Controls(m & "emp" & i).Value = c.Offset(i, 0).Value
.Controls(m & "grd" & i).Value = c.Offset(i, 2).Value
.Controls(m & "recap" & i).Value = c.Offset(i, 4).Value
Next i
End With
Set c = c.Offset(12, 0)
Next mNum
End Sub

asp.net Logic to check if user already exist

I have this code to check if a user already exists, but it is not working.
For some reason system is skipping this function:
if Cadastrar_CodEmpresa = verificar("Usuario") Then
Response.Redirect("index.asp?pagina=login")
Here is the code:
if request.Form("commentForm") = "sim" Then
set verificar = conexao.execute ("select * from empresas")
Cadastrar_CodEmpresa = request.Form("CodEmpresa")
Cadastrar_Segmento = request.Form("Segmento")
Cadastrar_Endereco = request.Form("Endereco")
Cadastrar_Bairro = request.Form("Bairro")
Cadastrar_Cidade = request.Form("Cidade")
Cadastrar_CEP = request.Form("CEP")
Cadastrar_Pais = request.Form("Pais")
Cadastrar_Contato = request.Form("Contato")
Cadastrar_Telefone = request.Form("Telefone")
Cadastrar_Email = request.Form("email")
if Cadastrar_CodEmpresa = verificar("Usuario") Then
response.Redirect("index.asp?pagina=login")
Else
set cadastrar_cadastro = conexao.execute("insert into Empresas (Usuario) Values ('" & Cadastrar_CodEmpresa & "')")
End if
... some more stuff ...
End if