title: Check if record exists before adding it to the database
body: <p>Is there a way in VBA that allows one to check if a record exists in a database before adding a record?</p>
  
  
  
  <p>I tried to capture a select query in a variable:</p>
  
  <pre class="lang-vb prettyprint-override"><code>sSQL = "select count(*) from OptionRestriction where Feature_ID_1 = 1021 AND OptionValue_1 = 3 AND value = 0 AND Feature_ID_2 = 403 AND OptionValue_2 = 5 and visible = 1"
  </code></pre>
  
  <p>I used the variable in an if statement to check whether the count is not 1. If the statement is true the record should be added:</p>
  
  <pre class="lang-vb prettyprint-override"><code>            If sSQL &lt;&gt; "1" Then
                  conn.Execute "INSERT INTO OptionRestriction (Feature_ID_1, OptionValue_1, value, Feature_ID_2, OptionValue_2, visible) SELECT TOP(1) CF.FeatureID As Feature_ID_1, CF.OptionValue  As OptionValue_1, 0, OV.FeatureID As Feature_ID_2, OV.OptionValue  AS OptionValue_2, 1 From Value as CF Cross Join Value as OV INNER JOIN Feature f on CF.FeatureID = f.ID INNER JOIN Feature f_2 on OV.FeatureID = f_2.ID Where CF.Name = '" &amp; Replace(s_OptionValue_1_s, "'", "''") &amp; "' AND OV.Name = '" &amp; Replace(s_OptionValue_2_s, "'", "''") &amp; "'"
              End If
  </code></pre>
  
  <p>However this won't work in VBA. Any ideas how I can make this work?</p>
  
  <h2>UPDATE</h2>
  
  <p>Based on the comments of Vityata, I made the following changes:</p>
  
  <p>I added a function:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Public Function RestrictionCount() As Long
       RestrictionCount = DCount("Feature_ID_1", "OptionRestriction", "Feature_ID_1 = 1023 AND OptionValue_1 AND value = 0  AND Feature_ID_2 = 403 AND optionValue_2 = 5 AND visible = 1")
  End Function
  </code></pre>
  
  <p>And used the functon in the code where my if resides:</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Sub CommandButton1_Click()
  
     Dim conn              As New ADODB.Connection
     conn.Open             "&lt;string to connect to DB. Not displayed here due to confidentiality reasons&gt;"
  
  
              If RestrictionCount() &lt;&gt; 1 Then
                  conn.Execute "INSERT INTO OptionRestriction (Feature_ID_1, OptionValue_1, value, Feature_ID_2, OptionValue_2, visible) SELECT TOP(1) CF.FeatureID As Feature_ID_1, CF.OptionValue  As OptionValue_1, 0, OV.FeatureID As Feature_ID_2, OV.OptionValue  AS OptionValue_2, 1 From Value as CF Cross Join Value as OV INNER JOIN Feature f on CF.FeatureID = f.ID INNER JOIN Feature f_2 on OV.FeatureID = f_2.ID Where CF.Name = '" &amp; Replace(s_OptionValue_1_s, "'", "''") &amp; "' AND OV.Name = '" &amp; Replace(s_OptionValue_2_s, "'", "''") &amp; "'"
              End If
  
  End Sub
  </code></pre>
  
  <p>Now I am getting a compile error stating that a sub or function is not defined, while the DCount resides in another function. Is there a way to solve this? Sorry for my questions (I am new to VBA).</p>
  
  <h2>UPDATE</h2>
  
  <p>The check can now be performed thanks to the last comment of Vityata. The code is now as follows, including the loop I am using to add my data to the database:</p>
  
  <pre class="lang-vb prettyprint-override"><code>    rs.ActiveConnection = conn
      rs.Open "select * from OptionRestriction where Feature_ID_1 = 1021 AND OptionValue_1 = 3 AND value = 0 AND Feature_ID_2 = 403 AND OptionValue_2 = 5 and visible = 1"
  
      i = 2
      Do Until IsEmpty(Cells(i, 1))
          s_OptionValue_1_s = ActiveSheet.Cells(i, 1)
          value_s = ActiveSheet.Cells(i, 2)
          s_OptionValue_2_s = ActiveSheet.Cells(i, 3)
  
          If value_s &lt;&gt; "" Then
  
              If rs.RecordCount &lt;&gt; 1 Then
                  conn.Execute "INSERT INTO OptionRestriction (Feature_ID_1, OptionValue_1, value, Feature_ID_2, OptionValue_2, visible) SELECT TOP(1) CF.FeatureID As Feature_ID_1, CF.OptionValue  As OptionValue_1, 0, OV.FeatureID As Feature_ID_2, OV.OptionValue  AS OptionValue_2, 1 From Value as CF Cross Join Value as OV INNER JOIN Feature f on CF.FeatureID = f.ID INNER JOIN Feature f_2 on OV.FeatureID = f_2.ID Where CF.Name = '" &amp; Replace(s_OptionValue_1_s, "'", "''") &amp; "' AND OV.Name = '" &amp; Replace(s_OptionValue_2_s, "'", "''") &amp; "'"
              End If
  
              conn.Execute "INSERT INTO OptionRestriction (Feature_ID_1, OptionValue_1, value, Feature_ID_2, OptionValue_2, visible) SELECT TOP(1) CF.FeatureID As Feature_ID_1, CF.OptionValue  As OptionValue_1, '" &amp; Replace(value_s, "'", "''") &amp; "', OV.FeatureID As Feature_ID_2, OV.OptionValue  AS OptionValue_2, 0 From Value as CF Cross Join Value as OV INNER JOIN Feature f on CF.FeatureID = f.ID INNER JOIN Feature f_2 on OV.FeatureID = f_2.ID Where CF.Name = '" &amp; Replace(s_OptionValue_1_s, "'", "''") &amp; "' AND OV.Name = '" &amp; Replace(s_OptionValue_2_s, "'", "''") &amp; "'"
          Else
              conn.Execute "INSERT INTO OptionRestriction (Feature_ID_1, OptionValue_1, value, Feature_ID_2, OptionValue_2, visible) SELECT TOP(1) CF.FeatureID As Feature_ID_1, CF.OptionValue  As OptionValue_1, 1, OV.FeatureID As Feature_ID_2, OV.OptionValue  AS OptionValue_2, 0 From Value as CF Cross Join Value as OV INNER JOIN Feature f on CF.FeatureID = f.ID INNER JOIN Feature f_2 on OV.FeatureID = f_2.ID Where CF.Name = '" &amp; Replace(s_OptionValue_1_s, "'", "''") &amp; "' AND OV.Name = '" &amp; Replace(s_OptionValue_2_s, "'", "''") &amp; "'"
              conn.Execute "INSERT INTO OptionRestriction (Feature_ID_1, OptionValue_1, value, Feature_ID_2, OptionValue_2, visible) SELECT TOP(1) CF.FeatureID As Feature_ID_1, CF.OptionValue  As OptionValue_1, 0, OV.FeatureID As Feature_ID_2, OV.OptionValue  AS OptionValue_2, 1 From Value as CF Cross Join Value as OV INNER JOIN Feature f on CF.FeatureID = f.ID INNER JOIN Feature f_2 on OV.FeatureID = f_2.ID Where CF.Name = '" &amp; Replace(s_OptionValue_1_s, "'", "''") &amp; "' AND OV.Name = '" &amp; Replace(s_OptionValue_2_s, "'", "''") &amp; "'"
          End If
  
      i = i + 1
      Loop
  </code></pre>
  
  <p>The result in the database is now:</p>
  
  <p><a href="https://i.stack.imgur.com/DlXLX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DlXLX.png" alt="enter image description here"></a></p>
  
  <p>Yet, the row that is selected should not be there. So it needs to add 1021 - 3 - 0 - 403 - 5 - 1 only once. What do I need to change in my loop to achieve this?</p>
  
  <p>The excel file from wich the data is inserted into the database looks like this:</p>
  
  <p><a href="https://i.stack.imgur.com/byAoN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/byAoN.png" alt="enter image description here"></a></p>