Output specific values from a textbox to multiple textboxes in VB - mysql

I am using VB with VS2015, and I have a textbox which receives about 40 lines of input from a microprocessor. What I ultimately need to do is display some of the input in multiple textboxes, as well as save those values into a mySQL database. For example, let's say I have:
SensorTypeA_Test
SensorA1_Output: 5.00
SensorA2_Output: 0
SensorA_Test: FAIL
SensorTypeB_Test
SensorB1_Output: 3.50
SensorB2_Output: 3.50
Sensor_Test: Pass
In this case, what I would like to do is basically have 6 textboxes (each with a pre-made label such as Sensor A1) fill with 5.00, 0, FAIL, 3.50, 3.50 and Pass.
As I see it, there are two ways to approach this. First, I could just figure out how to send the values straight to the mySQL database and then pull the needed values into the multiple textboxes. This would be fairly straight forward, but I don't know of a way to send these values directly to the database since they don't have variable names.
The other way is to either parse through the textbox itself or save the input to a file and parse through that. If I did it this way, I would prefer to have each textbox look for the value corresponding to its label, as opposed to filling all of the text boxes sequentially. What I mean by that is if there is a textbox labeled "Sensor A1", it would have its own variable name, say tbSensorA1 and it would look for "SensorA1_Output" and then show the value that comes after the colon, and if there is no value, then just display "N/A" in the textbox. This would also make it really easy to store the values in the database.
So basically, I'm wondering if there are any other ways to do this besides what I proposed, and if not, how can I search a textbox or a text file for a specific phrase and output the values which follow the colon in their corresponding textboxes? Any help is appreciated! Thanks!

Well - lots of ways to do this. I wouldn't recommend trying to do this in the database as databases are not great for parsing, and this feels like logic you should have in your middle or front end anyway. It might be totally front end if this is just really a way to display data you already have.
Here is one approach - create your text boxes that you desire, and in each text boxe's tag property - store a "Prefix" value that can be used to identify if a given sensor data value is appropriate for that text box.
Then you simply loop through each of the text boxes, and sensor data lines, do the comparison, then add the value to the text box if its a match.
Here is sample code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtSensor.Text = "SensorTypeA_Test()" & vbCrLf & _
"SensorA1_Output: 5.00" & vbCrLf & _
"SensorA2_Output: 0" & vbCrLf & _
"SensorA_Test: FAIL()" & vbCrLf & _
"SensorTypeB_Test()" & vbCrLf & _
"SensorB1_Output: 3.50" & vbCrLf & _
"SensorB2_Output: 3.50" & vbCrLf & _
"Sensor_Test: Pass()"
End Sub
Private Sub btnPopulate_Click(sender As Object, e As EventArgs) Handles btnPopulate.Click
txtSensorA.Tag = "SensorA"
txtSensorB.Tag = "SensorB"
Dim lstOutputs As New List(Of TextBox)
lstOutputs.Add(txtSensorA)
lstOutputs.Add(txtSensorB)
Dim sensorData() As String = txtSensor.Text.Trim.Split(vbCrLf)
For Each tb In lstOutputs
For Each sline In sensorData
If sline.TrimStart.StartsWith(tb.Tag) Then
UpdateOutputTextBox(tb, sline.Trim)
End If
Next
Next
End Sub
Private Sub UpdateOutputTextBox(tb As TextBox, sLine As String)
Dim sParts() As String = sLine.Split(":")
Dim sData As String = String.Empty
If sParts.Count = 2 Then
sData = sParts(1)
If tb.Text.Length > 0 Then
tb.Text &= (", " & sData.Trim)
Else
tb.Text = sData.Trim
End If
End If
End Sub
And the result...
So you should be able to do something very similar. Hope this helps.

Related

Editing listbox item

So i have a listbox that displays all the orders entered in the Order table using a select sql query. Now i also want to add the ability to edit the items from the listbox, i see the right click edit list item option but when i click it, it just opens the form but doesnt populate the fields. The form has textboxes that are unbound but i cant figure out how to get them to populate based on the right clicked entry. I have also tried to open the target form from vba and fill the fields through vba with the following code
Private Sub editrecordbttn_Click()
Dim valSelect As Variant
Dim v As Variant
Dim selector As Variant
Dim strValue As String ' just used for the demonstration
Dim splitvalue() As String
Dim selectedsampid As String
Dim selectedcusid As String
Dim Records As DAO.Recordset
Dim SQLcus As String
Dim SQLsamp As String
For Each valSelect In Me.searchlistbox.ItemsSelected
strValue = strValue & "" & Me.searchlistbox.ItemData(valSelect) & "," & "" & Me.searchlistbox.Column(1, valSelect) & ","
Next valSelect
' to remove trailing comma
strValue = Left(strValue, Len(strValue) - 1)
splitvalue() = Split(strValue, ",")
selectedsampid = splitvalue(0)
selectedcusid = splitvalue(1)
DoCmd.OpenForm ("Add Sample")
Forms![Add Sample].fnametxt.SetFocus
'query and fill cus info
SQLcus = "SELECT * FROM CustomerInfo WHERE CusID = '" & selectedcusid & "';"
Set Records = CurrentDb.OpenRecordset(SQLcus)
Me!clienttypetxt = Records![Client type].Value
End Sub
Ok, so say we have a listbox, and we do this:
The first column of the listbox is assumed to be the PK or "ID" of the rows.
so, we have this:
And thus you select a row, and then click on the button.
The button code would look like this:
Private Sub cmdEdit_Click()
Debug.Print "Hotel list id selected = " & Me.HotelList
DoCmd.OpenForm "frmEditHotels", , , "ID = " & Me.HotelList
End Sub
So, in most cases, for a better user experience, it probably better to approach things as per above.
There is of course the case in which you fill the listbox (or combo) with a "list" of values NOT from the database. In that case, you can use the "edit" list option. And this allows you to specify a form (or use the built in editor).
so, if this is NOT a list that you type in, and is from the database, then don't try to use the built in "list editing"
(add a button like above, and launch the form with the "where" clause to load the form to the ONE data record as I did above.
Since oh so very often, a listbox data will come from a table, then the edit list options are not really much particular use. And using a table (as opposed to a list) to fill + drive the combo/listbox is a much better design, and idea anyway.
This is especially the case if you ever want multiple-users, since the "list" edit feature would mean and suggest that each user editing the list would now have their own lists as opposed to using a table which everyone can edit.
Also, there is NO reason to use a loop to fill that list box. We can do this:
' setup critera for listbox.
Dim strSQL As String
' prompt user for Hotel city - we just hard code for this exmaple.
Dim strCity As String
strCity = "Banff"
strSQL = "SELECT ID,FirstName, LastName, City,HotelName FROM tblHotels " & _
"WHERE City = '" & strCity & "' " & "ORDER BY HotelName"
Me.HotelList.RowSource = strSQL
Note how we do not have some MESSAY value list, but can shove the data (sql) right to the listbox. Not only do we don't have loops, but we also don't have to worry about the size limits.
With "value list" (those messy delimited ";" list), then you have a rather small limit of 4,000 characters. Don't take many larger rows to "blow up" the listbox, since it can't handel very many rows.
In fact, I often still suggest you use the wizard to build the listbox, and you can choose a datasource (sql), or the MUCH lessor choice of "value list".
Value list is only a good choice if you have say a few choices like Mr., Mrs. or what not, and it not some large table, but only say 5-10 choices.
Anything larger? Use a data table driven listbox, and avoid use of value list.

Using textboxes to search entries based on a keyword (There are multiple textboxes for separate entities) and implementing a combo box

I want to be able to enter in a keyword into a textbox that would search records for that keyword and present reports based on that word that are found.
Please Note: There are multiple textboxes for this to separate specific information so that I need this to work for each individual textbox. There is one text box that will search for entries based on the date (month/day/year) and I have been having issues with it not displaying certain date ranges.
I am also looking to add in a combo box as well.
I have tried code that another individual has given me, but the issue that I have run into with the normal textboxes is that it requires me to put in everything that is in the entry as opposed to search based on a keyword to which nothing pops up when other criteria have been put in place (I know that there is an entry that fits, but for some reason when I add in the textbox element it does not work unless I put in the exact entry that is in the textbox that I am search the specific information from).
I have been trying to find information for combo boxes, but I have not found anything that really works with what I want to do.
Function SelectedItems(objBox As ListBox) As String
Dim strRtn As String, varItm
For Each varItm In objBox.ItemsSelected
strRtn = strRtn & ",'" & objBox.ItemData(varItm) & "'"
Next varItm
If strRtn <> vbNullString Then SelectedItems = Mid(strRtn, 2)
End Function
Private Sub Command62_Click()
Dim strSQL As String
Dim strArr As String
Dim varItm
For Each varItm In Array("District", "Circumstance", "Location", "Method", "Point", "Rank", "Description", "Missing", "IDNumber", "Dateto", "Datefrom", "Address")
strArr = vbNullString
Select Case Me.Controls(varItm).ControlType
Case acListBox
strArr = SelectedItems(Me.Controls(varItm))
Case acTextBox
If Not IsNull(Me.Controls(varItm).Value) Then
strArr = "'" & Me.Controls(varItm).Value & "'"
End If
End Select
If strArr <> vbNullString Then
strSQL = strSQL & "t." & varItm & " in (" & strArr & ") and "
End If
Next varItm
If strSQL <> vbNullString Then strSQL = "where " & Left(strSQL, Len(strSQL) - 5)
With CurrentDb.QueryDefs("qryMultiselect")
.SQL = "select * from tblDataEntry t " & strSQL
End With
DoCmd.OpenQuery "qryMultiselect"
End Sub
What I want is to be able to use my multi-select listboxes in conjunction with my textboxes as well as my combo box that I have as well. Using all of that or at least a combination of them, I want to be able to sort through my data to be able to create reports based on what is being looked for. Along with making sure that the user has the freedom to not having to enter in information for all fields and only the ones that they feel they need to enter in information on.
With the textboxes: I want to be able to search based on a keywords that corresponds with a specific section of the data that I am looking for and to be able to leave others blank and be able to pull up results.
The same goes for the date/time textboxes and my combo box.

Using OpenArgs to pass multiple textboxs into a different form

I am trying to pass 3 textboxes into a different form via parsing the string. I am getting a run time error 13.
Private Sub txtFullName_Click()
Const cstrForm As String = "frmInputInfo"
DoCmd.OpenForm "frmInputInfo", acFormAdd, , , acDialog, _
Me.txtFullName & "|" & Me.PATS_Job_Opening_ID & "|" & Me.NYCAPS_JobID
End Sub
Private Sub Form_Load()
varSplitString = Split(Me.OpenArgs, "|")
Me.[FullName].Value = varSplitString(0)
Me.[PATS Job Opening ID].Value = varSplitString(1)
Me.[NYCAPS_JobID].Value = varSplitString(2)
End Sub
and them on the form load I typed
Any help will be appreciated
You have to be extremely attentive with all those commas in the DoCmd.OpenForm options list. It's just way too darn easy to cause a misalignment between what you and Access think about which values apply to which options.
In your case you intend to pass a string, Me.txtFullName & "|" & Me.PATS_Job_Opening_ID & "|" & Me.NYCAPS_JobID, to OpenArgs. Unfortunately you omitted a comma, so Access thinks you're feeding it a value for WindowMode, which is supposed to be a number. Therefore, error 13: "type mismatch"!
Do it this way and you eliminate any confusion about which value goes with which option.
Dim strArgs As String
strArgs = Me.txtFullName & "|" & Me.PATS_Job_Opening_ID & "|" & Me.NYCAPS_JobID
Debug.Print strArgs ' make sure you got what you expect '
DoCmd.OpenForm FormName:="frmInputInfo", _
DataMode:=acFormAdd, _
WindowMode:=acDialog, _
OpenArgs:=strArgs
Also in the form event, make sure you got something for OpenArgs before you attempt to Split it. As it stands now, if the form is ever opened without supplying OpenArgs, your code will essentially attempt Split(Null, "|") and that will trigger a different error.
You can test before split like this:
If Len(Me.OpenArgs) > 0 Then
' do your split thing here '
End If

Hyperlink text disappears based on combo box value on continuous form

I'm having a bit of an unusual problem (or at least I think it's unusual). I have a continuous form that shows certain records based on a query that updates after I change the value in a combo box. I have a "View All" hyperlinked text box so that I can switch to seeing all of the records when I want to, but have discovered it only appears when I have selected a combo box value that has records. For example, if I want to find records assigned to Joe Shmo and there is one record assigned to him, the hyperlink appears. If I want to find records assigned to Susie Seashell and she has none assigned to her, it disappears. The catch is that if I click on the area of the screen where the hyperlink should appear, it still works as expected-- there is just nothing visible that would lead you to click there. The text field's visibility is on, and I even tried coding in a txtfield.visible=true and it did nothing. Any help would be appreciated!
Note: The hyperlink control source is ="View All". Not sure if that matters or not.
All of the VBA code used on this form is below:
Option Compare Database
Private Sub cboFindNotice_AfterUpdate()
getSearchResults
End Sub
Private Function getSearchResults()
Dim sql As String
Dim errMsg As String
'== Sets value of string 'sql'
sql = "SELECT tblNotice.ID, tblNotice.noticeSEIFnoticeNumber, tblNotice.noticeJurisdiction, tblNotice.noticeDueDate, tblNotice.noticeTitle, " _
& "tblAnalysts.[analyst_lName] & "", "" & [analyst_fName] AS Expr1, tblNotice.noticeStatus" _
& " FROM tblAnalysts INNER JOIN tblNotice ON tblAnalysts.ID = tblNotice.noticeAnalyst" _
& " WHERE tblNotice.noticeAnalyst LIKE '*" & Me.cboFindNotice & "*'" _
& " ORDER BY tblNotice.noticeDueDate"
'== Displays records based on query
Me.Form.RecordSource = sql
Me.Form.Requery
End Function
Private Sub noticeSEIFnoticeNumber_Click()
DoCmd.OpenForm "frmNoticeDetails"
[Forms]![frmNoticeDetails].Controls("txtControlField").Value = Me.Controls("ID").Value
Call Forms.frmNoticeDetails.txtControlField_AfterUpdate
End Sub
Private Sub txtViewAll_Click()
Me.cboFindNotice.Value = ""
Call cboFindNotice_AfterUpdate
End Sub

Databound CheckBoxList for Microsoft Access

In ASP.NET I have the option for building a CheckBoxList control from a data source where the values of the items can be the "Id" column (for example) while the text displayed next to the checkbox is from a "Name" column.
Is it possible to do something similar to this for Access 2003 with VBA? I am trying to avoid hardcoding the list of items if at all possible, but can't figure out how to do anything similar to this.
Just use a listbox. A list box in Access is great because it allows multiple columns, but will hide the first column as you ask. And you can just set the listbox to allow multiple selections (in the other tab of the property sheet for the listbox, set Multiselect to "simple"
And even better is you don't need any code to fill out the listbox, but can type in the sql or simply base the listbox on a SQL query.
So a listbox can look like this:
The code behind the button to turn the selected items into a useful list of PK id looks like this:
Private Sub cmdFax_Click()
Dim strIDList As String
Dim vID As Variant
Dim strSQLWhere As String
For Each vID In Me.lstFaxList.ItemsSelected
If strIDList <> "" Then strIDList = strIDList & ","
strIDList = strIDList & lstFaxList.ItemData(vID)
Next
If strIDList <> "" Then
strSQLWhere = " ID in (" & strIDList & ")"
End If
DoCmd.OpenReport "rptFax", acViewPreview, , strSQLWhere
End Sub
And if you want, you can in code for supply or set the SQL of the listbox like this:
Sub mytest()
Dim strSql As String
strSql = "Select ID, firstName, LastName, City " & _
"where city = 'Edmonton'"
Me.lstFaxList.RowSource = strSql
End Sub
So you don't need to type in any kind of list here. It not clear if you "must" have a actual check box here - you can use a continues form if you wanted, but I think the above is oh so much more simple and you don't need much code to set this up at all.