Note that there are some explanatory texts on larger screens.

plurals
  1. POBlock if without end if
    primarykey
    data
    text
    <p>I am very new to VBA. I tried to calculate median for a vector. The following code keeps getting warning regarding "Block if without End if". I tried to change the place of "End IF", but it resulted in another warning "Block end if without if". Your input would be appreciated. Thanks.</p> <pre><code>Sub CalculateMedian() DoCmd.SetWarnings False Dim db As DAO.Database Dim onet As DAO.Recordset Dim Ocode As String Dim ag As DAO.Recordset Dim agMedian As Integer Set db = CurrentDb 'select one variable in current database Set onet = db.OpenRecordset("SELECT DISTINCT ONetCode FROM Single WHERE LEN(ONetCode)&gt;8") Do While Not onet.EOF 'assigning value to a variable does not need a "SET" Ocode = onet.Fields("ONetCode") 'any data meet the criterion--&amp;Ocode&amp; can vary Set ag = db.OpenRecordset("SELECT AG FROM Single WHERE ONetCode='" &amp; Ocode &amp; "' ORDER BY AG") 'using .recordcount needs to use .movelast first ag.MoveLast ag.MoveFirst If ag.RecordCount Mod 2 = 1 Then agMedian = ((ag.RecordCount + 1) / 2) thecount = 0 Do While Not ag.EOF thecount = thecount + 1 If thecount = agMedian Then 'inset the result into a new table, and need to create a new table in advance DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" &amp; Ocode &amp; "'," &amp; ag("AG") &amp; ");") Exit Do End If If ag.RecordCount Mod 2 = 0 Then agMedian = ag.RecordCount / 2 thecount = 0 Do While Not ag.EOF thecount = thecount + 1 If thecount = agMedian Then m1 = ag("AG") ElseIf thecount = agMedian + 1 Then m2 = ag("AG") DoCmd.RunSQL ("INSERT INTO PCImedian(onetcode, agMedian) VALUES('" &amp; Ocode &amp; "'," &amp; ((m1 + m2) / 2) &amp; ");") Exit Do End If Loop DoCmd.SetWarnings True End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload