Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839

    Count Fields

    HB is recordset
    C = For Next count

    HB!["Equip_" & C] = Null

    Without count it would be:

    HB.Edit
    HB![Equip_1]
    HB![Equip_2]
    HB![Equip_3]
    HB![Equip_4]
    HB![Equip_5]
    HB![Equip_N…..]
    HB.Update


    HB! "[Equip_" & C * "]" = Null gets Invalid use of property.
    HB! ["Equip_" & C ] = Null gets Item not found in this collection

    Col_Name = "HB![Equip_" & C & "]"
    Col_Name = Null get invalid use of null






  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    In simple English what are you trying to do?
    An example might help readers understand the issue.

  3. #3
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Orange,
    Right now I am trying to build a simple loop through many fields just to clear data. Then I'll go from there

    I have 40 Fields in a table named Equip_1 through Equip_40, the column couny changes

    I have determined the column count as: CurrentDb.TableDefs("Table2").Fields.Count - 6

    C is an integer with a value starting at 1.

    Now I want to assign "" to each field

    Normally you would write ![Field_1] ="", ![Field_2] =""...….![Field_40] =""

    I think this is a bit excessive and can be shortened to a few lines.

    DO How would I insert the number value in?

    MSGBOX "Equip_" & C gives you - Equip_1 through 40 where C-1 to 40 in a for next loop. (When you want to click OK 40 times.)

    Now I am trying to insert string between the bookends to call the field I want to write to

    !["Equip_" & C], But it is the incorrect syntax.

  4. #4
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    you could try HB.Fields("Equip_" & C)=??

    No idea of how you're trying to set the value of a field by showing us HB![Equip_1]
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725

  6. #6
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Orange,
    I was giving field names (Column Names) to set the trend of naming. I am trying to clear data from the columns.

    So far I have this:

    Code:
    Dim db As DAO.Database
    Dim HB As DAO.Recordset
    Dim C As Integer, Col_Name As String
    Set db = CurrentDb()
    Set HB = CurrentDb.OpenRecordset("Table2")
        C = 1
    Do While Not HB.EOF
        HB.Edit
            For C = C To (CurrentDb.TableDefs("Table2").Fields.Count - 6)
        HB.Fields("Equip_" & C) = ""
        HB.Update
            Next C
    Loop
    
    End Sub 



    This does seem to loop since the error I get is "Update or CancelUpdate without AddNew or Edit. Even though I have .edit in there.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I can see some issues (such as not moving through the recordset) but why use complicated code when you could just use an update query?

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    ?? Still not sure what you're trying to do???

    Are you trying to delete records? OR
    Are you trying to remove values from the Equip_1 to 40 fields of ALL records in the table?

  9. #9
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Its a simple start to a more complicated thing.

    I know I could make a update query with 40 fields in it.

    Moving through the fields?

    Movnext?

  10. #10
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Quote Originally Posted by orange View Post
    ?? Still not sure what you're trying to do???

    Are you trying to delete records? OR
    Are you trying to remove values from the Equip_1 to 40 fields of ALL records in the table?

    remove values from the Equip_1 to 40 fields of ALL records in the table

  11. #11
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    I think a much better option would be to use an update query, you can build that in a VBA loop if you wish:

    Dim sSQL As String, sField As String, iCount As Integer


    sField = ""
    For iCount = 1 To 40
    sField = sField & "," & "[Eguip_" & iCount & "] = Null"
    Next iCount


    sField = Mid(sField, 2)
    sSQL = "UPDATE Table2 SET " & sField & ";"


    CurrentDb.Execute sSQL, dbFailOnError

    Cheers,
    Vlad

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,725
    I'd like to hear the rationale for a table with 40 fields named Equip_1 thru Equip_40.
    Vlad's solution looks good.

  13. #13
    Thompyt is offline Expert
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2014
    Location
    El Paso, TX
    Posts
    839
    Its setting up a Visio export table. There may be 40 recorded pieces of equipment on a platform.

    I think I understand how Vlads code works, but I need to think more on it, as I was expecting to do other things and didn't want to use the strSQL way.

    Thanks Vlad

  14. #14
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I think there was a question about my post. While not the way I'd do it, this is closer to what you had
    Code:
    Dim rs As DAO.Recordset
    Dim i As Integer
    
    Set rs = CurrentDb.OpenRecordset("tblYourTable")
    If Not (rs.EOF And rs.BOF) Then
      Do While Not rs.EOF
        With rs
          For i = 1 To rs.Fields.count - 1 'assumes the first fld to be excluded but all the rest included
            .Edit
            .Fields(i) = "" 'or Null or 0 or whatever
         Next
        .Update
        .MoveNext
        End With
      Loop
    End If
    It certainly will fail if there's any violation of data rules. The other code given isn't a query IMO, but is vba sql. I meant a stored query (query def). If you can bring yourself to create a table with fields named like that, an actual query shouldn't be out of scope either.
    What I wrote should work regardless of the number of fields or their names.

  15. #15
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Quote Originally Posted by Thompyt View Post
    I think I understand how Vlads code works,
    It should if all you have is one record to be updated. I reviewed but don't see anything where you said if there's only one record to be updated or if there's more.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Count where multiple fields are the same
    By Rriemer in forum Access
    Replies: 4
    Last Post: 06-21-2017, 01:31 PM
  2. How to count records from different fields?
    By Lightningkid in forum Queries
    Replies: 3
    Last Post: 02-14-2017, 02:37 AM
  3. Count fields
    By hithere in forum Reports
    Replies: 7
    Last Post: 04-22-2012, 02:15 PM
  4. not count date fields if the same
    By Icky_Joe in forum Queries
    Replies: 2
    Last Post: 04-11-2012, 12:55 PM
  5. two fields needing to look like one for a count how?
    By techexpressinc in forum Queries
    Replies: 2
    Last Post: 05-29-2009, 01:06 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