Results 1 to 25 of 25

Thread: [RESOLVED] Code works on buttonclick, but not automatically

  1. #1

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Resolved [RESOLVED] Code works on buttonclick, but not automatically

    Right now on my program I have it so when the user clicks on a certain item in the combobox, it moves to that item. All I am using is

    VB Code:
    1. Do Until ID.text = newValue
    2.      If newValue > oldValue then
    3.           Me.RecordSet.MoveNext
    4.      Else
    5.           Me.RecordSet.MovePrevious
    6.      End If
    7. Loop

    Now this works fine...It moves the bound items to the correct record, however, if I allow the form_current event to run directly after this, I receive the error

    Quote Originally Posted by Runtime Error '2115'
    The macro or function set to the BeforeUpdate or ValidationRule for this field is preventing [Beta] from saving data in the field.
    This happens on line

    VB Code:
    1. txtCourse.Text = ""

    However, if I run the above loop, and stop form_current from running. Then run a commandbutton with this code

    VB Code:
    1. Thinger = False
    2. Form_Current

    Where thinger is the variable I used to stop form_current from running.

    The program runs fine??


    This has been stumping me for a good number of hours already, and any help would be appreciated

  2. #2
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    Got quite a simple answer for you..

    Use a completely unbound form rather than trying to bind to the underlying table and hold unbound controls as well..

    You can still replicate it as tho it was actually bound by using recordset..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  3. #3

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    Yeah I had that before...But binding some of it was a lot less code

    *sigh* okay, guess I gotta get to work

    Thanks for the reply

  4. #4
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    To be honest I started out with unbound forms, which makes it easier for me as the majority of the code is second knowledge now..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  5. #5

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    Okay, I switched everything to unbound. I am using ADO/SQL to populate everything, but I am still gettign the same error

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    It it's not too much code could you post the code behind the form...

    It could also be a table restriction that's causing this
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    Can't post the whole form...But here is what I do moving from record to record (works fine)

    VB Code:
    1. Private Sub cmdNext_Click()
    2.      rsWorkerForm.MoveNext
    3.      Form_Current
    4. End Sub

    And here is what I am doing when I click the combobox and attempt to move over a record

    VB Code:
    1. Private Sub ID_Click()
    2.      newValue = ID.Text
    3.      ID.Value = oldValue
    4.      Do Until rsWorkerForm.Fields("ID") = newValue
    5.           If newValue > oldValue Then
    6.               Dunno = True
    7.               rsWorkerForm.MoveNext
    8.           ElseIf newValue < oldValue Then
    9.               Dunno = True
    10.               rsWorkerForm.MovePrevious
    11.           End If
    12.      Loop
    13.      Dunno = False
    14.      Form_Current
    15. End Sub

    The snippet directly above errors.

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    Let me shot together an example Access db to determine the correct method..

    You could also post the Form_Current code as it might be something in there..

    Are you writing the data back to the table between recordset move??
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    No, I'm not writing back to the table at all. And sure, here is the code for the Form_Current, although I dont see why moving over 1 record wouldnt error, but moving over 2+ would :/

    VB Code:
    1. Private Sub Form_Current()
    2. If Not RsLoad Then
    3.     RsLoad = True
    4.     Form_Load
    5. End If
    6. Dim strQuery As String
    7. 'strQuery = "SELECT * FROM WorkerInformation WHERE ID ='" & ID.Value & "'"
    8. 'Dim rsQuery As ADODB.Recordset
    9. 'Set rsQuery = New ADODB.Recordset
    10. 'rsQuery.Open strQuery, cn
    11. ID.SetFocus
    12. ID.Value = rsWorkerForm.Fields("ID")
    13. txtName.SetFocus
    14. txtName.Text = rsWorkerForm.Fields("Name")
    15. Account.SetFocus
    16. Account.Text = rsWorkerForm.Fields("Account")
    17. Anniversary.SetFocus
    18. Anniversary.Text = rsWorkerForm.Fields("Anniversary")
    19. cboClassifications.Value = rsWorkerForm.Fields("Classification")
    20. cboClassifications_Change
    21. If cboClassifications.Value = "Support" Then
    22.     Level.SetFocus
    23.     Level.Text = rsWorkerForm.Fields("Level")
    24.     Payband.SetFocus
    25.     Payband.Text = rsWorkerForm.Fields("Payband")
    26.     Hours.SetFocus
    27.     Hours.Text = rsWorkerForm.Fields("Hours")
    28. ElseIf cboClassifications.Value = "Academic" Then
    29.     Payband.SetFocus
    30.     Payband.Text = rsWorkerForm.Fields("Payband")
    31.     MaxPayband.SetFocus
    32.     MaxPayband.Text = rsWorkerForm.Fields("MaxPayband")
    33. Else
    34.     txtSalary.SetFocus
    35.     txtSalary.Text = rsWorkerForm.Fields("FullSalary")
    36. End If
    37.  
    38. Saving = True
    39. Campustotal = 0
    40. AnniversaryHappened = False
    41. anniversaryCurrent = False
    42. Call cboClassifications_Change
    43. Account.SetFocus
    44. Select Case Account.Text
    45. Case "42000", "42500", "41200", "40404", "41201"
    46.     Level.Visible = True
    47.     Level_Label.Visible = True
    48. Case Else
    49.     Level.Visible = False
    50.     Level_Label.Visible = False
    51. End Select
    52. Call SetVars
    53. ID.SetFocus
    54. ChangeMade = False
    55. txtCourse.SetFocus
    56. txtCourse.Text = ""
    57. txtSplit.SetFocus
    58. txtSplit.Text = ""
    59. txtStartDate.SetFocus
    60. txtStartDate.Text = ""
    61. txtEndDate.SetFocus
    62. txtEndDate.Text = ""
    63. test = 1
    64. Dim rsNumber As ADODB.Recordset
    65. Set rsNumber = New ADODB.Recordset
    66. ID.SetFocus
    67. strThing = "SELECT SplitsNumber FROM Splits WHERE ID = " & "'" & ID.Text & "'"
    68. rsNumber.Open strThing, cn, adOpenKeyset, adLockPessimistic, adCmdText
    69.     NumoSplits = rsNumber.Fields("SplitsNumber")
    70. rsNumber.Close
    71. testing
    72. ID.SetFocus
    73. oldValue = ID.Text
    74. End Sub

    **Note, there is some redundant code in there, from other attempts.**

  10. #10
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    First thing.. lose all the setfocus calls as they are not needed.. reference the default Value on the controls instead

    E.G.

    VB Code:
    1. ID.SetFocus
    2. ID.Value = rsWorkerForm.Fields("ID")

    can just be

    VB Code:
    1. ID.Value = rsWorkerForm.Fields("ID")

    And it will still work..

    Still looking... looking at adding a running count on the query of the form...
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  11. #11

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    cool, didn't know you could do that. I always referenced the .text property

  12. #12
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    Right the sequential count on the records seems to allow the Recordset.Move to work correctly...

    What you need to do is take the combobox's field and perform a sequence count on this... so change the form's SQL to include this.. Presuming ID is the combo

    SeqNr: Dcount("ID","WorkerInformation","ID<" ID)+1
    If text based then
    SeqNr: Dcount("ID","WorkerInformation","ID<'" ID & "'")+1

    Now the killer part...

    Remove all the code from the Form_Current event..
    Create a Private Long Variable say lngCurrRec
    Change the ID combobox to have the following RowSource, needs two columns with the second one hidden..

    SELECT ID, DCount("ID","WorkerInformation","ID<'" & [ID] & "'")+1 AS SeqNr FROM WorkerInformation

    On The Form Load set the lngCurrRec = 1
    On The Form Activate Set the combobox to return the first instance of ID

    On The ComboBox Change do the following

    VB Code:
    1. 'populate the controls on the form with the seqnr set
    2.   Dim lngVal As Long
    3.   lngVal = CLng(IDCombo.Column(1))
    4.   If lngVal < CurrentNr Then
    5.     Recordset.Move -(CurrentNr - lngVal)
    6.   Else
    7.     Recordset.Move (lngVal - CurrentNr)
    8.   End If
    9.   CurrentNr = lngVal

    And hopefully that should be Job done..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  13. #13
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    Forgot to mention the form will need to be based on an SQL to allow the recordset.move event to work correctly..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  14. #14

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    Okay, I am assuming lngCurrentRec and CurrentNr are the same? Otherwise I don't see a use for lgnCurrentRec..


    Also, why must I remove everything from form_current?


    Ill try working on this right now :/

  15. #15
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    Your assumption is correct.. got lazy and just copied the information from what I'd built..

    Your form_current is trying to write the data from the recordset back into the form.. which could be causing the error.. if the form is based on an SQL statement then the Recordset.Move will handle that for you..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  16. #16

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    I originally used SQL to connect to the DB and that didn't work as well... Note the commented code in my form_current.

    Or is there another way to connect using only SQL and not using ADO at all?

  17. #17
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    You running this from VB6 or Access.. Here is a small example of what I mean..
    Attached Files Attached Files
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  18. #18

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    im using VBA (Access)

    If that is a vb6 program I cant run it for the record.

    And also,

    VB Code:
    1. lngVal = CLng(ID.Column(1))

    Wouldnt work, so I changed it to

    VB Code:
    1. lngVal = CLng(ID.Column(1,CurrentNr))

    Can you foresee any problems with me doing this?

  19. #19
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Code works on buttonclick, but not automatically

    Not it's purely Access.. only got Office and .Net at work..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  20. #20

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Code works on buttonclick, but not automatically

    Well, thank ya kindly. This works pretty good, although it only moves back 1 record if lngValue < CurrentNr..But I'm sure I can fix that

    Thanks for all your help

  21. #21
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: [RESOLVED] Code works on buttonclick, but not automatically

    I got it working correctly between 23 records moving to number 23 and then back to 1..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  22. #22

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: [RESOLVED] Code works on buttonclick, but not automatically

    thats weird..Because using the code you posted above..Mine only moves 1 record in each direction....

  23. #23
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: [RESOLVED] Code works on buttonclick, but not automatically

    Check your SeqNr in the RowSource of the Combo... sounds like it ain't correct.. it should return a count of the records irrespective of autonumber.. in my example Db I removed autonumber 5 to display this..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  24. #24
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: [RESOLVED] Code works on buttonclick, but not automatically

    You actually don't need the SeqNr in the SQL Query just in the ComboBox source..

    Any changes made to the entries in the form will be reflected back to the table by the way.. so you might want to lock them if not allowing edits
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  25. #25

    Thread Starter
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: [RESOLVED] Code works on buttonclick, but not automatically

    Thanks again..after referencing your project I got it working perfectly

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width