Sub NUMEROPEDIDO
OpSql.Initialize
Dim sf As StringFunctions
sf.Initialize
DBCursor = OpSql.SQL1.ExecQuery("SELECT rowid, * FROM pedidos ORDER BY rowid DESC ") 'contacts_table = table name in the database
If DBCursor.RowCount > 0 Then
DBCursor.Position = 0
NP = sf.Right("NP-000" & sf.Trim(sf.Val(sf.Right(DBCursor.GetString("rowid"), 13)) + 1), 13)
Log("NUMERO DE PEDIDO : " & NP)
Else
NP = "NP-0001"
Log("NUMERO DE PEDIDO : " & NP)
End If
End Sub
Dim NP As String = "NP-0000"
Dim strID As String = DBCursor.GetString("rowid")
NP = NP.SubString2(0,7-strID.Length) & strID
Albert Thank you for answering I'll try it!OK, try this:
B4X:Dim NP As String = "NP-0000" Dim strID As String = DBCursor.GetString("rowid") NP = NP.SubString2(0,7-strID.Length) & strID
So substring is zero based, but the 2nd value is also absolute position in the string less 1.
(so in theory, that number should be 6, but we have to +1 to it).
Don't think you need any string handling library code.
Regards,
Albert D. Kallal
Edmonton, Alberta Canada
Public Sub TestFormatter
Dim out As String
out = out & CRLF & SetFormmatter("NP-", "12345")
out = out & CRLF & SetFormmatter("NP-", 3.45)
out = out & CRLF & SetFormmatter("NP-", "123")
out = out & CRLF & SetFormmatter("NP-", 2456000.3)
Log(out)
End Sub
Public Sub SetFormmatter( Prefix As String, Value As String) As String
Dim formatter As B4XFormatter
formatter.Initialize
formatter.GetDefaultFormat.GroupingCharacter = ""
formatter.GetDefaultFormat.DecimalPoint = ""
formatter.GetDefaultFormat.MaximumFractions = 0
formatter.GetDefaultFormat.MinimumFractions = 0
formatter.GetDefaultFormat.Prefix = Prefix
formatter.GetDefaultFormat.IntegerPaddingChar = "0"
formatter.GetDefaultFormat.MinimumIntegers = 8
Return formatter.Format(Value)
End Sub
Hey Oparra thank you so much I'll try it!Use B4XFormatter
[B4X] B4XFormatter - Advanced number formatter
B4XFormatter is an alternative to NumberFormat / NumberFormat2 keywords. It is implemented in B4X and it is cross platform. There are two types in the library: B4XFormatter - The main class. B4XFormatData - A type with various configurable fields. The formatter holds a list of format data...www.b4x.com
B4X:Public Sub TestFormatter Dim out As String out = out & CRLF & SetFormmatter("NP-", "12345") out = out & CRLF & SetFormmatter("NP-", 3.45) out = out & CRLF & SetFormmatter("NP-", "123") out = out & CRLF & SetFormmatter("NP-", 2456000.3) Log(out) End Sub Public Sub SetFormmatter( Prefix As String, Value As String) As String Dim formatter As B4XFormatter formatter.Initialize formatter.GetDefaultFormat.GroupingCharacter = "" formatter.GetDefaultFormat.DecimalPoint = "" formatter.GetDefaultFormat.MaximumFractions = 0 formatter.GetDefaultFormat.MinimumFractions = 0 formatter.GetDefaultFormat.Prefix = Prefix formatter.GetDefaultFormat.IntegerPaddingChar = "0" formatter.GetDefaultFormat.MinimumIntegers = 8 Return formatter.Format(Value) End Sub
View attachment 119177
other:
Formattare con separat migliaia e virgola decimali (modo italiano)
Provo a guardarlo al volo Come non detto. Magari dopo la partita, visto che gli esempi di @oparra funzionano bene e sono l'ideale, essendo B4X e multipiattaforma.www.b4x.com
Hey udg thank you I'll try itDid you try
B4X:Dim NP as String = $"NP-${NumberFormat2(rowid,4,0,0,False)}"$
OK, try this:
B4X:Dim NP As String = "NP-0000" Dim strID As String = DBCursor.GetString("rowid") NP = NP.SubString2(0,7-strID.Length) & strID
So substring is zero based, but the 2nd value is also absolute position in the string less 1.
(so in theory, that number should be 6, but we have to +1 to it).
Don't think you need any string handling library code.
Regards,
Albert D. Kallal
Edmonton, Alberta Canada
Well I've been trying like this:Hey udg thank you I'll try it
OpSql.Initialize
DBCursor = OpSql.SQL1.ExecQuery("SELECT rowid, * FROM pedidos ORDER BY rowid DESC ") 'contacts_table = table name in the database
Dim rowid As Long = DBCursor.GetLong("rowid")
Dim NP As String = $"NP-${NumberFormat2(rowid,4,0,0,False)}"$
error:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
OpSql.Initialize
DBCursor = OpSql.SQL1.ExecQuery("SELECT rowid, * FROM pedidos ORDER BY rowid DESC ") 'contacts_table = table name in the database
If DBCursor.RowCount > 0 Then
DBCursor.Position = 0
Dim NP As String = "NP-0000"
Dim strID As String = DBCursor.GetString("rowid")
NP = NP.SubString2(0,7-strID.Length) & strID
Log("NUMERO DE PEDIDO : " & NP)
Else
Log("NUMERO DE PEDIDO : " & NP)
End If
I tried this:Did you try
B4X:Dim NP as String = $"NP-${NumberFormat2(rowid,4,0,0,False)}"$
OpSql.Initialize
DBCursor = OpSql.SQL1.ExecQuery("SELECT rowid, * FROM pedidos ORDER BY rowid DESC ") 'contacts_table = table name in the database
If DBCursor.RowCount > 0 Then
DBCursor.Position = 0
'NP = sf.Right("NP-000" & sf.Trim(sf.Val(sf.Right(DBCursor.GetString("rowid"), 13)) + 1), 13)
Dim NP As String = $"NP-${NumberFormat2(DBCursor.GetString("rowid"),4,0,0,False)}"$
Log("NUMERO DE PEDIDO : " & NP)
End If
DBCursor = OpSql.SQL1.ExecQuery("SELECT rowid, * FROM pedidos ORDER BY rowid DESC LIMIT 1;") 'contacts_table = table name in the database
If DBCursor.RowCount > 0 Then
DBCursor.Position = 0
Dim crowid As Long = DBCursor.GetLong("rowid")
Dim NP As String = $"NP-${NumberFormat2(crowid+1,4,0,0,False)}"$
Log("NUMERO DE PEDIDO : " & NP)
Else
NP = "NP-0001"
End If
This behavior would be sub-optimal for invoice numbers.If the rowid is not aliased by INTEGER PRIMARY KEY then it is not persistent and might change.
Yes you right I shoud be using a resultSet instead. The ORDER BY I can remove that isn't a issueYou should use a ResultSet instead of a Cursor.
Anyway, in your post #9 you don't cover the case of an empty table (rowcount = 0); that's the place where you would put NP equal to NP-0001, if I understand you correctly
When it comes to a table with a least one row, you can
- limit the result to one row (since you have an ORDER BY desc)
- pick that rowid,
- add 1
- use it in NumberFormat2
Hey Jeffrey thank you for you reply. It's a common table. I'm not using an autoincrement as a Primary Key I just use the rowid inside the table , and it is well defined. Thank youNot sure if this is relevant, as I'm not that familiar with SQLite, but how have you defined your table?
This behavior would be sub-optimal for invoice numbers.
@oparra and @udg gave some nice options. And if you want another method to do it all within SQLite instead of using an external function. here it is:Thank you
Dim rs As ResultSet = SQL.ExecQuery("SELECT 'NP-' || printf('%04i',rowid) as rid, * FROM pedidos ORDER BY rid DESC")
Do While rs.NextRow
log(rs.getstring("rid"))
Loop
Hey Mahares thank you for answering! I'll try it@oparra and @udg gave some nice options. And if you want another method to do it all within SQLite instead of using an external function. here it is:
In the resultset: 1 becomes NP-0001, 18 becomes NP-0018, 502 becomes: NP-0502, etc.B4X:Dim rs As ResultSet = SQL.ExecQuery("SELECT 'NP-' || printf('%04i',rowid) as rid, * FROM pedidos ORDER BY rid DESC") Do While rs.NextRow log(rs.getstring("rid")) Loop
Sub NUMEROPEDIDO
OpSql.Initialize
DBCursor = OpSql.SQL1.ExecQuery("SELECT MAX(rowid) as ID FROM pedidos ORDER BY rowid DESC")
If DBCursor.RowCount > 0 Then
DBCursor.Position = 0
Dim NP As String = "NP-0000000"
Dim strID As String = DBCursor.GetString("ID")+1
NP = NP.SubString2(0,10-strID.Length) & strID
Log("NUMERO PEDIDO : "&NP)
Else
Dim fakerowid As String = 1
NP = "NP-0000000"
NP = NP.SubString2(0,10-fakerowid.Length) & fakerowid
Log("NUMERO PED : "&NP)
End If
End Sub
Hey Daniel: You really did not need to make it complicated by using a cursor or resultset at all. Here is what you full code should have been:I was able to fix it
Sub NUMEROPEDIDO
Dim strID As String= SQL.ExecQuerySingleResult("SELECT max(rowid)+1 FROM pedidos ")
Dim NP As String
If strID > 1 Then
NP = "NP-0000000"
NP = NP.SubString2(0,NP.Length-strID.Length) & strID
Log("NUMERO PEDIDO : " & NP)
Else
NP = "NP-0000001"
End If
End Sub
Here it is all in ONE line of code and tested. Try it. If it does not do what you want, I will admit Messi is a better player that Ronaldo.Thanks to all of you.
Log( IIf(SQL.ExecQuerySingleResult($"SELECT max(rowid) FROM pedidos"$) > 0 , _
SQL.ExecQuerySingleResult($"SELECT 'NP-' || printf('%07i', max(rowid)+1) FROM pedidos"$) , "NP-0000001"))
It Works when my table pedidos has data.. but if it empty it shows me this error:Hey Daniel: You really did not need to make it complicated by using a cursor or resultset at all. Here is what you full code should have been:
B4X:Sub NUMEROPEDIDO Dim strID As String= SQL.ExecQuerySingleResult("SELECT max(rowid)+1 FROM pedidos ") Dim NP As String If strID > 1 Then NP = "NP-0000000" NP = NP.SubString2(0,NP.Length-strID.Length) & strID Log("NUMERO PEDIDO : " & NP) Else NP = "NP-0000001" End If End Sub
OpSql.Initialize
DBCursor = OpSql.SQL1.ExecQuery("SELECT rowid, * FROM pedidos ORDER BY rowid DESC")
If DBCursor.RowCount > 0 Then
DBCursor.Position = 0
Dim crowid As Long = DBCursor.GetLong("rowid")
Dim NP As String = $"NP-${NumberFormat2(crowid+1,7,0,0,False)}"$
Log("NUMERO PEDIDO : "&NP)
Else
Dim fakerowid As String = 1
NP = "NP-0000000"
NP = NP.SubString2(0,10-fakerowid.Length) & fakerowid
Log("NUMERO PED : "&NP)
End If
End Sub
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?