Results 1 to 7 of 7
  1. #1
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38

    Arrow Add a New Record based on Inputbox with Loop

    Hello Smart Users,

    I am a newbie in access. Still trying to learn access.

    I have a table with 2 field named tblNumber. ID (PK, Autonumber) Number (Number, Long Integer)

    I have a form (frmMy) with a text box (text1) default_value used as DMAX to get MAX value of tblNumber =Nz(DMax("Number","tblNumber"),0) & one command button

    This is what i would like to achieve, when i click on button;

    1. Pop up an Input Box asking for "How many records want to insert?"
    2. Check the input box response, if it >0 then
    3. Insert text1+response numbers in table tblNumber. Of Course need to check in tblNumber before add records for duplicates. SImply can exclude duplicates & insert only non duplicates numbers.
    4. In a another text box ( I can create in this form, ie text2) showing, "The following Accounts are created"

    eg:

    let's say MAX value in text1 is 1000, when prompt for input box, I entered 5



    So in tblNumber need to INSERT new records as 1001, 1002, 1003, 1004, 1005 & text2 showing "The following Accounts are created: 1001, 1002, 1003, 1004, 1005")

    Also, how can i force text1 to update when i open form frmMy?

    I really appreciate any help.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Instead of popup box, suggest a textbox on form. Use Validation Rule to assure the input is a number and/or use code to check value.

    Something like:

    Dim i As Integer
    Dim strNumbers As String
    Dim intNumber As Integer
    intNumber = Nz(DMax("Number","tblNumber"),0)
    For i = 1 to Me.text1
    intNumber = intNumber + 1
    CurrentDb.Execute "INSERT INTO tblNumber(Number) VALUES(" & intNumber & ")"
    strNumbers = strNumbers & intNumber & ',"
    Next
    Me.text2 = strNumbers

    Why would there be duplicates?

    Is Text1 bound to a field? It should not be. Use DMax expression in ControlSource property.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38
    Thank you June7 for your reply.

    Please disregard about duplicates. Sorry for that. As you said, I have created text3 for input.

    When I click on button, "Run-time error '3134'", syntax error in INSERT INTO.

    This is what i have;

    Private Sub cmdClick_Click()
    Dim i As Integer
    Dim strNumbers As String
    Dim intNumber As Integer
    intNumber = Nz(DMax("Number", "tblNumber"), 0)
    For i = 1 To Me.Text3
    intNumber = intNumber + 1
    CurrentDb.Execute "INSERT INTO tblNumber(Number) VALUES(" & intNumber & ")"
    strNumbers = strNumbers & intNumber & ","
    Next
    Me.Text2 = strNumbers
    End Sub


    Thank you for your help.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    The syntax is correct, it works for me. I tested with a Long Integer field. Something else is the real issue. Maybe the DMax. Test with a hard-coded value in place of the DMax and see what happens.

    Review link at bottom of my post for debugging guidelines. Step debug. Follow the code as it executes. Do the variables get populated as expected?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Everything else aside, I have to wonder about a Field named 'Number,' which is a Reserved Word in Access. Your say your code is choking on the line

    CurrentDb.Execute "INSERT INTO tblNumber(Number) VALUES(" & intNumber & ")"

    and that Field name may be the choking point. The name needs to be changed, regardless.

    Linq ;0)>

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    I wondered if it might be a reserved word but creating the field doesn't trigger message like using Name does. I thought I successfully tested with Number as field name but just tried again and it does fail. Enclose in [] then it does work.


    CurrentDb.Execute "INSERT INTO tblNumber([Number]) VALUES(" & intNumber & ")"
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Only4Access is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Feb 2012
    Posts
    38
    Sorry for the late response.

    Thank you June for your help, as well Linq letting about reserved word.

    I changed integer to Long in
    Dim intNumber As Integer to deal with large numbers. When I tested As Integer; with numbers >40000 shows overflow error.

    Thanks again for all your help.

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

Similar Threads

  1. Replies: 2
    Last Post: 11-15-2013, 09:58 AM
  2. Replies: 8
    Last Post: 09-19-2013, 03:07 PM
  3. Loop function not moving to next record
    By jax1000rr in forum Programming
    Replies: 3
    Last Post: 02-26-2013, 12:21 PM
  4. Loop until last record in excel table
    By compooper in forum Programming
    Replies: 5
    Last Post: 06-30-2011, 02:14 PM
  5. update table in loop based on IF statement
    By LALouw in forum Access
    Replies: 0
    Last Post: 07-26-2009, 08:46 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