Results 1 to 11 of 11
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    dcount if has required else and within area

    I have a form unbound textbox which with

    Code:
    =DCount("NewSchoolsID","tblSchools","AreaID=" & [AreasID])
    gives me a count of the schools within the area

    however

    I would like a count of the schools in the area that have an enrollment over 100 (from [Enrollment] field)

    I thought of having an IF so that IF it didn't count it would show 0

    not sure how to do this with a dcount - all I get are errors.

    I thought something like this would work

    =DCount("NewSchoolsID","tblSchools","AreaID=" & [AreasID] And "Enrollment">100)



    This doesn't work for me either

    =DCount("NewSchoolsID","tblSchools","Enrollment >=100" And "AreaID=" & [AreasID])

    This will count all the records above 100

    =DCount("NewSchoolsID","tblSchools","Enrollment >=100")

    but I also need to narrow it down by area at the same time

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Is there supposed to be an s in [AreasID])

    The other thing is your Dcount is going to return a number. Do you have something to store it in?
    Maybe try this.

    Dim lngID As Long
    lngID = Me.AreaID
    Dim lngCount As Long
    lngCount = DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & lngAreaID)
    msgBox lngCount

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I used this

    =DCount("NewSchoolsID","tblSchools","Enrollment >=100" & "And AreaID=" & [AreasID])

    not sure if it is correct though if someone could let me know...

  4. #4
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    Is there supposed to be an s in [AreasID])

    The other thing is your Dcount is going to return a number. Do you have something to store it in?
    Maybe try this.

    Dim lngID As Long
    lngID = Me.AreaID
    Dim lngCount As Long
    lngCount = DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & lngAreaID)
    msgBox lngCount
    I didn't get a chance to respond before posting again, it doesn't need to store - only display.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Well does the code I offer give you a message box? That it the first step to make sure it works. Then you can edit the code to assign the result to wherever, textbox, data field.

  6. #6
    ipisors is offline Access Developer
    Windows XP Access 2007
    Join Date
    Sep 2013
    Posts
    119
    I don't see any absolute necessity to have a variable here, you can certainly use Dcount() as an expression in a textbox's control source.

    Try:

    =Iif(DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & [AreasID])>100, DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & [AreasID]),0)

    However, I do agree that in these situations you ought to code VBA, not use an expression. Because for starters, what I posted makes 2 trips to the data, this would be better - on the Form Current event, or maybe load event, depending on the usage of the form.

    Code:
    Dim lngCount as Long
    lngCount=DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & [AreasID])
    If lngCount>100 then
       Me.Controlname.Value=lngCount
    else
      Me.Controlname.Value=0
    End If

  7. #7
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by ipisors View Post
    I don't see any absolute necessity to have a variable here, you can certainly use Dcount() as an expression in a textbox's control source
    I actually missed or forgot the part where the unbound textbox was mentioned. I was reading the other part of the OP's post and started believing he was in the VBE. I posted the extra lines of code so the OP would have a way to step through it. Your example is more complete.

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    I actually missed or forgot the part where the unbound textbox was mentioned. I was reading the other part of the OP's post and started believing he was in the VBE. I posted the extra lines of code so the OP would have a way to step through it. Your example is more complete.
    Cheers thanks for the response etc

  9. #9
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ipisors View Post
    I don't see any absolute necessity to have a variable here, you can certainly use Dcount() as an expression in a textbox's control source.

    Try:

    =Iif(DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & [AreasID])>100, DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & [AreasID]),0)

    However, I do agree that in these situations you ought to code VBA, not use an expression. Because for starters, what I posted makes 2 trips to the data, this would be better - on the Form Current event, or maybe load event, depending on the usage of the form.

    Code:
    Dim lngCount as Long
    lngCount=DCount([NewSchoolsID], "tblSchools", "[AreaID=]" & [AreasID])
    If lngCount>100 then
       Me.Controlname.Value=lngCount
    else
      Me.Controlname.Value=0
    End If
    But would this work on a continuous form? I would have to make it step through each record to work...?

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    The current event is only going to fire when you click on the record by selecting it or a control within the form.

    Sounds like your up against running a report to view the summary in the format you want.

  11. #11
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ItsMe View Post
    The current event is only going to fire when you click on the record by selecting it or a control within the form.

    Sounds like your up against running a report to view the summary in the format you want.
    Yeah that is why I made it an expression as apposed to using VBA. No data is needed to be kept. When the person opens the form they need only see what is current for that area to assess which staff member should be allocated to what area. So if the area displays many schools in number (these are the schools that are over 100 enrolled) then the manager can allocate who thinks would be best suited to handle that area.

    There isn't much point in storing the value because I won't be doing anything with it. Since it's a continuous form and I need all the areas to display their count as soon as the form is opened I have used an expression. The vba I am guessing which would give the same result won't execute for all records until that record is selected (even on a continuous form).

    I'm pretty much going to say this is solved. I'm getting the results I need etc.

    Thanks for the help!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2015, 05:49 PM
  2. Elapsed Time by Area
    By sathishj1981 in forum Queries
    Replies: 1
    Last Post: 03-20-2013, 02:08 AM
  3. Texas City,State,Zip and Area Code
    By burrina in forum Sample Databases
    Replies: 0
    Last Post: 11-12-2012, 10:35 PM
  4. Detail area of form help
    By Darkmatter5 in forum Forms
    Replies: 1
    Last Post: 07-10-2012, 04:28 PM
  5. Sums in Detail area of report
    By Rick West in forum Reports
    Replies: 2
    Last Post: 12-01-2009, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums