在将其添加到数据库之前,检查是否存在logging

在VBA中有没有一种方法可以在添加logging之前检查数据库中是否存在logging?

我试图捕获一个variables中的select查询:

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" 

我在if语句中使用variables来检查count是不是1.如果语句是真的,则应该添加logging:

  If sSQL <> "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 = '" & Replace(s_OptionValue_1_s, "'", "''") & "' AND OV.Name = '" & Replace(s_OptionValue_2_s, "'", "''") & "'" End If 

但是这在VBA中不起作用。 任何想法,我可以做这个工作?

UPDATE

基于Vityata的意见,我作了如下修改:

我添加了一个函数:

 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 

在我所在的代码中使用了functon:

 Private Sub CommandButton1_Click() Dim conn As New ADODB.Connection conn.Open "<string to connect to DB. Not displayed here due to confidentiality reasons>" If RestrictionCount() <> 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 = '" & Replace(s_OptionValue_1_s, "'", "''") & "' AND OV.Name = '" & Replace(s_OptionValue_2_s, "'", "''") & "'" End If End Sub 

现在我得到一个编译错误,说明一个子或函数没有定义,而DCount驻留在另一个函数。 有没有办法解决这个问题? 对不起,我的问题(我是VBA新手)。

UPDATE

现在可以进行检查,感谢Vityata的最新评论。 现在代码如下,包括我用来将数据添加到数据库的循环:

  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 <> "" Then If rs.RecordCount <> 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 = '" & Replace(s_OptionValue_1_s, "'", "''") & "' AND OV.Name = '" & Replace(s_OptionValue_2_s, "'", "''") & "'" 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, '" & Replace(value_s, "'", "''") & "', 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 = '" & Replace(s_OptionValue_1_s, "'", "''") & "' AND OV.Name = '" & Replace(s_OptionValue_2_s, "'", "''") & "'" 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 = '" & Replace(s_OptionValue_1_s, "'", "''") & "' AND OV.Name = '" & Replace(s_OptionValue_2_s, "'", "''") & "'" 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 = '" & Replace(s_OptionValue_1_s, "'", "''") & "' AND OV.Name = '" & Replace(s_OptionValue_2_s, "'", "''") & "'" End If i = i + 1 Loop 

现在数据库中的结果是:

在这里输入图像说明

然而,被选中的行不应该在那里。 所以它只需要添加一次1021 – 3 – 0 – 403 – 5 – 1。 我需要改变我的循环来实现这个目标?

将数据插入数据库的excel文件如下所示:

在这里输入图像说明

如果你在MS Excel中工作,这是一个你可以使用的样本:

 Public Sub CheckDataInSQLServer() Dim cnLogs As Object Dim rsData As Object On Error GoTo CheckDataInSQLServer_Error Set cnLogs = CreateObject("ADODB.Connection") Set rsData = CreateObject("ADODB.Recordset") cnLogs.Open ConnectionString 'this is a string function rsData.ActiveConnection = cnLogs rsData.Open "SELECT * FROM A WHERE B = C;" If rsData.EOF Then debug.print "no values" Else debug.print "has values" End If End Sub 

在VBA中,您需要的function称为DCount ,如果您使用MS Access

您可以使用DCount函数来确定指定logging集(一个域)中的logging数。 使用Visual Basic中的DCount函数,macros,查询expression式或计算的控件。 例如,您可以在模块中使用DCount函数来返回Orders表中与特定date的订单相对应的logging数。 MSDN

查看更多如何使用2个条件的 DCount – DCount

这是一个示例如何在DCount中构buildDCount并使用它:

 Public Function OrdersCount(ByVal strCountry As String, ByVal dteShipDate As Date) As Long OrdersCount = DCount("[ShippedDate]", "Orders", _ "[ShipCountry] = '" & strCountry & _ "' AND [ShippedDate] > #" & dteShipDate & "#") End Function If OrdersCount("Bulgaria", Now()) = 0 then 'Execute the SQL query. End if