Android Question Updating a Label from a Sub Function

algPoS

Member
Hi everyone

WELL I have taken the plunge and have started Working on our Android Hand held application

So far it is going ok we have 60% of the screen redone and working on the Zebra TC55 Hand held

But I am stuck on a function

In Windows Mobile I was allowed to have all my Database functions in one sub file and I could call them from any part of the program and update labels.

But in B4A its crashes if I try the following

B4X:
Public Sub LegalCheck_FindIteminDatabase(BarcodeNumber As String)
    Dim SQLCommand As String = ""
    
    SQLCommand = SQLCommand + "Select "
    SQLCommand = SQLCommand + "posdescription "
    SQLCommand = SQLCommand + "from producttable "
    SQLCommand = SQLCommand + "where barcodenumber='" + BarcodeNumber + "';"
    
End Sub

And just so I know the code has worked I wanted to update the label2 on the b4XLegalCheck (and the Desgin screen is called LegalCheck) but when ever I try I crash the App

I have tried to use b4XLegalCheck.label2.text = "This is from the DB Sub" and I have tried LegalCheck.label2.text = "This is from the DB Sub" and nothing

I have even tried to do it as a Return function and that also crashes my app.

I would like to get this working as when I scan items and recall the data from the database the Product name is to be shown on label2 (example if I scan 50201600 then the App would show "Cadburys Cream Egg" on the label.

Does anyone know what I am doing wrong I can attach my full project if that helps as it is only forms at the moment and not much code.
 
Solution
I just downloaded your project, but unfortunately it does not work because I get an error ROBOCOPY does nit find a sp‚cifi file.
Your project is a B4XPages project, and to zip it you must use this line on top of the B4XMainPage module.

View attachment 157235

Can you please upload the new zip file.

From what i understand, you want to access Label2 from the B4XLegalCheck module from the DatabaseFunction module.
I suggest you to try the following:
In the B4XLegalCheck module add the Label2 in the calling routine:
B4X:
Private Sub EditText1_EnterPressed
    DatabaseFunctions.LegalCheck_FindIteminDatabase(EditText1.Text, Label2)
End Sub
And in the DatabaseFunction module add it in the routine.
B4X:
Public Sub...

aeric

Expert
Licensed User
Longtime User
LegalCheck_FindIteminDatabase
This is a bad function signature.
The left side of the underscore is same as a view's name and the right side can denotes the event's name.

How to you declare the view?
B4X:
Private LegalCheck As Label
or
B4X:
Private LegalCheck As B4XView
Either way, you can just assign the value by using the Text method/property.
B4X:
LegalCheck.Text = BarcodeNumber

Not sure how you write your app. Usually, you can scan the barcode and assign it to a EditText view. You can use the TextChanged event.
B4X:
Private Sub EditText1_TextChanged (Old As String, New As String)
    Log(New)
End Sub

From the value you got, you can do a query to the database and retrieve the required fields.
 
Upvote 0

algPoS

Member
sorry first off for the slow replay I've been away from the office all day.

@aeric
How would you layout the function name I'm still using the formats that I was using in VB so if I need to do it in another format then I would be happy to change

At the moment the label is declared
as Private LegalCheck As Label

do I need to change to another that so it can be access form a code module?

@klaus
Sorry about the + on the SQLCommand for some reason I tried to use SQLCommand += like I can in Gambas and VB but it was not happy (but then I was doing a lot of things at once so should that last way work)

I shall be happy once I can get the label to change to say "hello from database su
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
sorry first off for the slow replay I've been away from the office all day.

@aeric
How would you layout the function name I'm still using the formats that I was using in VB so if I need to do it in another format then I would be happy to change

At the moment the label is declared
as Private LegalCheck As Label

do I need to change to another that so it can be access form a code module?

@klaus
Sorry about the + on the SQLCommand for some reason I tried to use SQLCommand += like I can in Gambas and VB but it was not happy (but then I was doing a lot of things at once so should that last way work)

I shall be happy once I can get the label to change to say "hello from database su
If you are new to B4X, I suggest you learn this language by reading the booklet written by @klaus

B4X in general is very similar to VB6 or VB.NET.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Post your project, or a small one showing the problem, because I do not really understand what exactly you want to do.
In your first post you say that the application is crashing, you should post the message in Logs Tab.
Then you speak about a Label, but there is no Label in your code.
Without seeing what you have done and how it is impossible to give any concrete advice.
 
Upvote 0

algPoS

Member
B4X in general is very similar to VB6 or VB.NET.

I have a very long time using VB6 and VB.net so I can understand and work with them also I have done 4 years in QBASIC 4.5 and PDS 7.1 (both are for PURE MS-DOS systems)

Post your project, or a small one showing the problem, because I do not really understand what exactly you want to do.
In your first post you say that the application is crashing, you should post the message in Logs Tab.
Then you speak about a Label, but there is no Label in your code.
Without seeing what you have done and how it is impossible to give any concrete advice.
Please find attached my full project (as I have no company code in it yet I am happy upload it.)

To replicate what I am trying to do please do as follows

from the main menu select "Legal Check" and then key in any random number and then if it works you would see the message "From Database Sub Function" show on the Legal Check form


But as I said I am not sure how I share the label so it can be access from the LegalCheck_FindIteminDatabase sun function of the databaseFunction module

BUT I am open to any ideas and suggestions you all have


Andy
 

Attachments

  • MobileApp.zip
    48 KB · Views: 21
Upvote 0

aeric

Expert
Licensed User
Longtime User
I am still not very clear what is your question.
If you don't know how to read from a database, check the SQL tutorial.


You also exporting the B4XPages project wrong.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I guess this is what you wanted.

1727095283366.png

B4XLegalCheck:
Private Sub EditText1_EnterPressed
    'DatabaseFunctions.LegalCheck_FindIteminDatabase(EditText1.Text)
    Label2.Text = DatabaseFunctions.FindItemInDatabase(EditText1.Text)
End Sub

DatabaseFunctions:
Public Sub FindItemInDatabase (BarcodeNumber As String) As String
    Dim strSQL As String = $"SELECT posdescription
    FROM producttable
    WHERE barcodenumber = ?"$
    Dim ItemDescription As String
    Dim rs As ResultSet
    rs = Main.SQL1.ExecQuery2(strSQL, Array As String(BarcodeNumber))
    Do While rs.NextRow
        ItemDescription = rs.GetString("posdescription")
    Loop
    rs.Close
    Return ItemDescription
End Sub
 

Attachments

  • MobileApp.zip
    49.9 KB · Views: 19
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I just downloaded your project, but unfortunately it does not work because I get an error ROBOCOPY does nit find a sp‚cifi file.
Your project is a B4XPages project, and to zip it you must use this line on top of the B4XMainPage module.

1727093079463.png


Can you please upload the new zip file.

From what i understand, you want to access Label2 from the B4XLegalCheck module from the DatabaseFunction module.
I suggest you to try the following:
In the B4XLegalCheck module add the Label2 in the calling routine:
B4X:
Private Sub EditText1_EnterPressed
    DatabaseFunctions.LegalCheck_FindIteminDatabase(EditText1.Text, Label2)
End Sub
And in the DatabaseFunction module add it in the routine.
B4X:
Public Sub LegalCheck_FindIteminDatabase(BarcodeNumber As String, Label2 As Label)
    Dim SQLCommand As String = ""
    
    SQLCommand = SQLCommand & "Select "
    SQLCommand = SQLCommand & "posdescription "
    SQLCommand = SQLCommand & "from producttable "
    SQLCommand = SQLCommand & "where barcodenumber='" & BarcodeNumber & "';"
    
    
    'This should then change the Label2 in the B4XLegalCheck / LegalCheck Page
    ' to say the Word "From Database Sub Function"
    '
    'This is just so I can work out how to share data between modules as label2
    'will be used to show the product Description from the database Once I have
    'Worked out how to connect to the DB Directly (as this app will only run
    'on a internal network only no External access allowed
    Label2.Text =
    
End Sub
Here, you can access Label2 directly.
I have not tested it.

Another suggestion where ever you can use B4XViews instead native views.
Example for Panel, Label and EditText views, these are cross - platform and have some more properties.
 
Upvote 0

algPoS

Member
I guess this is what you wanted.

View attachment 157240
B4XLegalCheck:
Private Sub EditText1_EnterPressed
    'DatabaseFunctions.LegalCheck_FindIteminDatabase(EditText1.Text)
    Label2.Text = DatabaseFunctions.FindItemInDatabase(EditText1.Text)
End Sub

DatabaseFunctions:
Public Sub FindItemInDatabase (BarcodeNumber As String) As String
    Dim strSQL As String = $"SELECT posdescription
    FROM producttable
    WHERE barcodenumber = ?"$
    Dim ItemDescription As String
    Dim rs As ResultSet
    rs = Main.SQL1.ExecQuery2(strSQL, Array As String(BarcodeNumber))
    Do While rs.NextRow
        ItemDescription = rs.GetString("posdescription")
    Loop
    rs.Close
    Return ItemDescription
End Sub
That is just what I wanted thank you

so that does mean I can have all my database function in one location and not have is spread around the app (like I had to with the ce version)
 
Upvote 0

algPoS

Member
I just downloaded your project, but unfortunately it does not work because I get an error ROBOCOPY does nit find a sp‚cifi file.
Your project is a B4XPages project, and to zip it you must use this line on top of the B4XMainPage module.

View attachment 157235

Can you please upload the new zip file.

From what i understand, you want to access Label2 from the B4XLegalCheck module from the DatabaseFunction module.
I suggest you to try the following:
In the B4XLegalCheck module add the Label2 in the calling routine:
B4X:
Private Sub EditText1_EnterPressed
    DatabaseFunctions.LegalCheck_FindIteminDatabase(EditText1.Text, Label2)
End Sub
And in the DatabaseFunction module add it in the routine.
B4X:
Public Sub LegalCheck_FindIteminDatabase(BarcodeNumber As String, Label2 As Label)
    Dim SQLCommand As String = ""
   
    SQLCommand = SQLCommand & "Select "
    SQLCommand = SQLCommand & "posdescription "
    SQLCommand = SQLCommand & "from producttable "
    SQLCommand = SQLCommand & "where barcodenumber='" & BarcodeNumber & "';"
   
   
    'This should then change the Label2 in the B4XLegalCheck / LegalCheck Page
    ' to say the Word "From Database Sub Function"
    '
    'This is just so I can work out how to share data between modules as label2
    'will be used to show the product Description from the database Once I have
    'Worked out how to connect to the DB Directly (as this app will only run
    'on a internal network only no External access allowed
    Label2.Text =
   
End Sub
Here, you can access Label2 directly.
I have not tested it.

Another suggestion where ever you can use B4XViews instead native views.
Example for Panel, Label and EditText views, these are cross - platform and have some more properties.
I shall do that when I get to the office pc that is running B4A
 
Upvote 0
Solution

algPoS

Member
Well, from tomorrow i will be absent from the form for 5 days, i am on travel.
Thank you for your help I hope you have a safe travel.

I have it working I have attached some picture to show you all the results of your help

I am so thankful now i know how to get this to work I can press on with the other screen and database functions

Thank you once again everyone

PS How do I mark the question as solved?
 

Attachments

  • IMG_6837.jpeg
    IMG_6837.jpeg
    34 KB · Views: 25
  • IMG_6838.jpeg
    IMG_6838.jpeg
    34.2 KB · Views: 26
Upvote 0
Top