0

I'm having a problem with DCount in VBA.

I'm trying to fill an array with values calculated by Dcount. One of the criteria I use in the Dcount I want to get from a different array, this gives the problem:

Dim i As Integer
For i = 0 To (intNumberOfSplitterboxes - 1)
    Dim arrFreeSingleModePorts(243) As Variant
    arrFreeSingleModePorts(i) = 
        DCount("ID", "SplitterboxPorts", _
        "SplitterboxID=i AND Mode='SingleMode' AND IsNull(DevicePortID) AND IsNull(SplitterboxPortID)")
Next

I get error 2471

2
  • declare your array variable outside the loop. DCount has incorrect string see below answer Commented Jul 21, 2015 at 12:33
  • Can you run this query successfully? SELECT Count(ID) FROM SplitterboxPorts WHERE SplitterboxID=1 AND Mode='SingleMode' AND IsNull(DevicePortID) AND IsNull(SplitterboxPortID) Commented Jul 21, 2015 at 23:37

1 Answer 1

0

You have included your i variable inside of your string. To use the actual value of i in your string you need to concatenate them together like this: "SplitterboxID=" & i & " AND Mode='SingleMode' AND IsNull(DevicePortID) AND IsNull(SplitterboxPortID)"

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. I tried your solution. I understand what the problem is. But now I get a different error: Error 13, types don't match.
Does this error occur on the line arrFreeSingleModePorts(i) = DCount("ID", "SplitterboxPorts", _ "SplitterboxID=i AND Mode='SingleMode' AND IsNull(DevicePortID) AND IsNull(SplitterboxPortID)")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.