PDA

View Full Version : [SOLVED:] seond button +1 won't work



hendrikbez
01-14-2016, 12:47 AM
Hi, I did get this code form Jonh (thank you), did some changes on this, it is working for me, but when I want to add 2 buttons to do the same, I get error, if I press the first button, is put info on second textbox.

I am trying to understand this code, but I am not good at this

The first code is working.



Private Sub addnumber_1_Click()
DoCmd.GoToRecord , , acLast
NewRec_StrFldPlus1 "Stickernumber"
End Sub
Private Function NewRec_StrFldPlus1(ctlname As String) As Boolean
On Error Resume Next

Dim val1 As String, val2 As Long
val1 = Me.Controls(ctlname)
If NumAtEnd(val1, val2) Then

'if control value ends in a number, +1 and create a new record
DoCmd.GoToRecord
Me.Controls(ctlname) = Replace(val1, val2, val2 + 1)
End If
End Function

Private Function NumAtEnd(s As String, num As Long) As Boolean
Dim n, i As Integer
For i = Len(s) To 1 Step -1
Select Case Mid(s, i, 1)
Case 0 To 9
n = Mid(s, i, 1) & n
NumAtEnd = True
Case Else: Exit For
End Select
Next
num = n
End Function

Private Sub KeepNumberSame_Click()
DoCmd.GoToRecord , , acLast
NewRec_StrFldPlus2 "Stickernumber"
End Sub

Private Function NewRec_StrFldPlus2(ctlname As String) As Boolean
On Error Resume Next

Dim val1 As String, val2 As Long
val1 = Me.Controls(ctlname)
If NumAtEnd(val1, val2) Then

'if control value ends in a number, +1 and create a new record

DoCmd.GoToRecord , , acNewRec
Me.Controls(ctlname) = Replace(val1, val2, val2 + 0)
End If
End Function


This don't work, my second textbox is only numbers




Private Sub Booknumber_1_Click()
DoCmd.GoToRecord , , acLast
NewRec_StrFldPlus3 "BookNumber"
End Sub


Private Function NewRec_StrFldPlus3(ctlname1 As String) As Boolean
On Error Resume Next

Dim val1 As String, val2 As Long
val1 = Me.Controls(ctlname)
If NumAtEnd(val1, val2) Then

'if control value ends in a number, +1 and create a new record
DoCmd.GoToRecord
Me.Controls(ctlname) = Replace(val1, val2, val2 + 1)
End If
End Function

Private Sub keepBook_0_Click()
DoCmd.GoToRecord , , acLast
NewRec_StrFldPlus4 "BookNumber"
End Sub

Private Function NewRec_StrFldPlus4(ctlname1 As String) As Boolean
On Error Resume Next

Dim val1 As String, val2 As Long
val1 = Me.Controls(ctlname)
If NumAtEnd(val1, val2) Then

'if control value ends in a number, +1 and create a new record
DoCmd.GoToRecord
Me.Controls(ctlname) = Replace(val1, val2, val2 + 0)
End If
End Function

jonh
01-14-2016, 05:20 AM
For some reason you've added 1 to ctlname.

ctlname1 isn't used anywhere as far as I can see.

hendrikbez
01-14-2016, 05:45 AM
I have changed it to ctlname.

So when I press the button (+1) for say "QWE1234", it put the new number in the textbox "QWE1235", but when I press the button (+1) say 12345, it clear the first button textbox and only update the first 4 numbers in my second textbox. I am not understanding this so far.

jonh
01-14-2016, 09:29 AM
The functions use gotorecord so you're jumping to another record when you click the buttons. That's what you asked for.

But you can't use that code to update multiple fields at once without modifying it. Instead of just one ctlname, you need an array of ctlnames.

Look at your data and you might see what's happening.

It might be better if created a picture of what you need and upload that.

hendrikbez
01-14-2016, 10:03 PM
Hi

On my screenshot you can see.

Container (Sticker) button (sticker#+1) when press this button it must get last record for this textbox and add one number
Container (Sticker) button (sticker#Keep Number Same) when press this button it must get last record for this textbox and use it here,but don't +1 number

Container (Book) button (Book#+1) when press this button it must get last record for this textbox and add one number
Container (book) button (Book # Keep number the same) when press this button it must get last record for this textbox and use it here,but don't +1 number

15183

jonh
01-15-2016, 02:46 AM
Clicking any of those buttons should add a new record. That new record will then be the 'last record'.

So if you click Sticker+1 it will copy the last value for Sticker into a new record.

If you then press Book+1 it will copy the last value for Book into another new record but Book doesn't have a value because Sticker+1 didn't set it.

There can only be one 'last record' and it changes whenever you add a new record.

So you either need to be more specific about the record you are copying data from or collect all of the data you need from the 'last record' before pasting it into a new record.


Your form doesn't make much sense. If I was a user I would expect button Sticker+1 to copy the value from the record I was currently on which is not necessarily the last record.

Maybe this is what you are asking?

In my original code I added a comment...


Private Sub Command_Click()

'Uncomment the line below to use the value of the LAST record,
'otherwise it uses the value of the CURRENT record

'DoCmd.GoToRecord , , acLast
NewRec_StrFldPlus1 "txtMyControl" '<-- CHANGE THIS TO YOUR CONTROL

End Sub
If you want to use the value that is currently being displayed and NOT the value from the last record comment gotorecord out.

hendrikbez
01-15-2016, 02:55 AM
Jonh

Thank you I understand that you can only have one last record, so I will then manual add the book record when I doing this.

Thank you