Loop through Textboxes in access database - database

I have an Access database that I want to loop through certain textboxes in order to do a calculation and display the answer in a seperate textbox. When I attempt to loop it loops through every control on my form (with the Form.Controls) method. I would like to only loop through 4 specific textboxes (txtbx1, txtbx2, txtbx3, txtbx4) when my button is clicked.
Explanation...
TextBox_A contains a Number
Upon button click take the number from TextBox_A, Multiply by 2800, then Divide by 12
Display the answer to the calculation in txtbx1.
I would do this for each of the 4 textboxes named above. Then have a "Total" textbox that adds up the total from each textbox (txtbx1, txtbx2, txtbx3, txtbx4). Please help, new to this and at a complete loss.

Every control has got a Tag property. Set the Tag property for textboxes whose values you want to include in your algorithm (say to "INClUDE"). It's free format text, so you can put what you like.
Then write code attached to some form event similar to this:
Dim c As Control
Dim txt As TextBox
For Each c In Me.Controls
'check it's a text box ...
If TypeOf c Is TextBox Then
'see if including ...
Set txt = c
If Len(txt.Tag) > 0 Then
'do something here (I've coloured, to show works)
txt.BackColor = 10
End If
End If
Next c
I've set an extra variable txt to refer to the textbox in question, so that I can get at the TAG property using autocompletion (I believe this is called a narrowing conversion!).

It would be easier if you rename TextBox_A (_B / _C etc) to TextBox_1 / _2 / _3 etc.
And then try this:
Dim i As Integer
For i = 1 To 4
Controls("txtbx" & i) = Val(Controls("TextBox_" & i)) * 2800 / 12
Next
Otherwise, this would work too, but it is not well readable:
= Val(Controls("TextBox_" & Chr(Asc("A") + i - 1))) * 2800 / 12

Related

Check if a string contains all other strings

I am trying to code a part of a software where I try to show the results that match search criteria.
I have a textbox where I can type one or more words I want to search and a listview that contains 4 different columns and a dozen rows. The idea is that each listview row contains lots of words and I want to see only the rows that contain all the words I have typed in the textbox. I have finished the code that searches for one term only. The problem I am having is that I don't fully understand how to do the same, but using multiple terms instead of one term only.
In the textbox, I write the words I want to search separated by a space. I have a variable where I keep the whole content of the listview row separated by : (example => col1row1content:col1row2content:col1row3content,etc). Summarizing, I want to check if a string (the full content of a row) contains all other strings (each word I have typped in the textbox).
This is the code I have implemented:
Dim textboxFullContentArray As String() = textboxSearch.Split(New Char() {" "c})
Dim Content As String
Dim containsAll As Boolean = False
Dim wholeRowContent(listviewMain.Items.Count - 1) As String ' each index of the array keeps the entire row content (one array contains all 4 cells of the row)
' wholeRowContent contains in one index the entire content of a row. That means,
' the index contains the 4 cells that represent an entire row.
' The format is like "rowData1:rowData2:rowData3:rowData4" (omitted for simplicity)
For Q As Integer = 0 To listviewMain.Items.Count - 1
For Each Content In textboxFullContentArray
If wholeRowContent(Q).ToLower.Contains(Content) Then
containsAll = True
' rest of the code...
ElseIf Not wholeRowContent(Q).ToLower.Contains(Content) Then
containsAll = False
Exit For
End If
Next
Next
But of course, this code is showing false positives and I think it's not a good solution. I think it must be much easier and I am overcomplicating the concept.
I am using VB.Net 2013
You can determine whether a String contains all of a list of substrings with a single line of code:
If substrings.All(Function(s) str.IndexOf(s, StringComparison.OrdinalIgnoreCase) >= 0) Then
Notice that I have actually implemented a case-insensitive comparison, rather than using ToLower or ToUpper.
It may not seem as neat to call IndexOf rather than Contains but guess what: Contains actually calls IndexOf internally anyway:
public bool Contains(string value)
{
return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}
You can write your own extension methods if you want a case-insensitive Contains method:
<Extension>
Public Function Contains(source As String,
value As String,
comparisonType As StringComparison) As Boolean
Return source.IndexOf(value, comparisonType) >= 0
End Function
Your If/Else looks like it could be simplified. I would set your containsAll value to true outside the nested loops, and only if you encounter a "Content" in "textboxFullContentArray" that is not contained in wholeRowContent(Q) you set containsAll to false, otherwise do nothing.
Also, one way to see what's going on is to print statements with the values that are being compared throughout your function, which you can read through and see what is happening at runtime when the false positives occur.
After some hours looking for a simple and effective solution (and trying different codes), I have finally found this solution that I adapted from: Bad word filter - stackoverflow
For Q As Integer = 0 To listviewMain.Items.Count - 1
If textboxFullContentArray.All(Function(b) wholeRowContent(q).ToLower().Contains(b.ToLower())) Then
' my code
End If
Next

How to pass the value of picturebox image to another picturebox using array vb.net

I'm beginner at vb.net please help me
How can i pass my picturebox1.image to picturebox2.image and the value of picturebox2.image to picturebox3.image. My point is, Everytime the new image arrive it's like the images moving forward.the incoming image will go to picturebox1. And the old value of picturebox1 will move to picturebox2 and the value of picturebox2 will go to picturebox3. I don't know how to construct the logic in coding. Help me please
Ok, there are a number of ways the accomplish this. I use my database to store images all the time, so I'll show you a method for displaying the images in the order they are retrieved from the database. You are correct in that if you assign pic2.image to pic1.image and so on, the picture boxes will all have the same image because that's the value of pic1, reversing the order does the same thing. You need to assign the image you want in each picture box to that picture box.
First I create a list of images, they're all jpg's but it shouldn't matter:
Imports System.Drawing ' <---- I assume you have this at the top of the code behind
Dim MyImages as List(of Image)
I retrieve the images from the database in a datatable in order of the primary key ascending, key is 1 to n and fill the list:
Dim x as Integer = 1
pictureboxname = "pic" & x
For Each MyDataRow as DataRow in MyTable.Rows
' Load the list, the image is in column zero
MyImages.Add(MyDataRow(0))
' because this is the initial list, you can also populate the pictureboxes too
' you need to use the picturebox name in an expression to populate the pictureboxes in a loop.
' Something like this .... this is untested
(pictureboxname).Image = MyImages(MyImages.Count -1)
' and change picturebox name to the next picture box
x += 1
' I assume picturebox name is pic1, pic2, etc ....
pictureboxname = "pic" + x
' you'll have to limit this loop to the number of pictureboxes you have if they are less than the number of images, but you get the idea
Next
When a new images is added, you can then add it to the list and database at the same time and then repopulate the picture boxes.
StoreImage(NewImage)
MyImages.Add(NewImage)
Dim x as integer = 1
pictureboxname = "pic" & x
For each MyImage as Image in MyImages
' assign each image in turn to the picture boxes ... again, untested
(pictureboxname).Image = MyImage
x += 1
pictureboxname = "pic" & x
' again, you'll have to limit this loop to the number of pictureboxes you have if it is less than the number of images ....
Next
This is not be exactly what you're looking for, but should get you started.

Loop textboxes and cells

I need some help making this code shorter. I have a lot of textboxes with times in my form which I want to copy to my Excel file. Some of the boxes needed to be 15min less in Excel sheet and I want to loop through these textboxes and and copy the result into the correct cell. My code works but I want to make it for more textboxes at once.
Dim d1re1 As Date = TextBox5.Text
TextBox5.Text = d1re1.ToLongTimeString()
Dim d1nre1 As Date = d1re1.AddMinutes(-15)
xlsp1.Cells(7, 100) = d1nre1.ToLongTimeString
Dim d1re2 As Date = TextBox7.Text
TextBox7.Text = d1re2.ToLongTimeString()
Dim d1nre2 As Date = d1re2.AddMinutes(-15)
xlsp1.Cells(7, 102) = d1nre2.ToLongTimeString
The idea here that the lines somehow doing the same but with different parameters.
Here functions come in the picture. You can just create generic method call it for ex : SetDateToFieldscand takes the parameters : the source textbox and which cell it needs to update.
Like this you just need only to call the method with different params.

Allow edits from different users appear in different color inside memo box : Microsoft Access 2010

I have a memo field which contains rich text. I am able to identify a user and change all the text in the box instead of just the text they added.
I am looking to write code which allows the text to be edited and after update , the edited text will appear a different color than the original text in the memo field.
I have tried :
Dim strNew As String
Dim strOld As String
If Me.txt_username_id = "grant" Then
strOld = Me.Form!txtnotesaboutproduct1.OldValue.ForeColor = vbBlack<br/>
strNew = Me.Form!txtnotesaboutproduct1.ForeColor = vbRed
End If
I have also tried
Dim ctlOld As TextBox<br/>
Set ctlOld = Me.Form!txtnotesaboutproduct1
If Me.txt_username_id = "grant" Then
ctlOld = Me.Form!txtnotesaboutproduct1.OldValue.ForeColor = vbRed
End If
Generally, I do this with a continuous subform for Notes, so that I can hold the data, date and user, rather than just one formatted text box. Though I do realize this might be a lot more real estate that you might have, you can use a conditional format within the subform. I do agree that if it is possible, you'll likely need to use HTML and not .Forecolor, which will change the entire box.

VBA: Variable length array from user input (6 optional text boxes)

I have a UserForm with 6 text-boxes, which the user can fill (or not) 2, 3, 4, or 6 or whatever he/she wants. These are called:
eb1, eb2, eb3, ...
At the end of the form there is also an 'accept' buttom, and when the user clicks it a text should appear as follows:
eb1, eb2, eb3, eb4...
Of course one of the problems is that if the user only fills 3 boxes, the result might be:
eb1, eb2, eb3, , ,
How can I do this? I guess I need something like a variable array and a for loop, but no idea how to do that, I am pretty new to VBA.
Thanks a lot.
Try below.
Create user form with 6 textboxes & name them as eb1, eb2, eb3, eb4, eb5 & eb6. Then add command button & paste below code to it. Then run the userform & enter some values to text boxes.
Click on the command button to see the msgbox.
Private Sub CommandButton1_Click()
dim str as string
dim a as integer
'Check for text box name & loop through textboxes
for a=1 to 6 'No of text boxes have.(remember that your text boxes should have names like eb1
'find out if there is any blank textboxes & avoid them
if Controls("eb" & a).Value <> "" then
'Correction for front , if eb1 is empty
if str="" then
str=Controls("eb" & a).Value
else
str=str & " , " & Controls("eb" & a).Value
end if
end if
next
msgbox str
end Sub
Final messagebox will give you the result you want. (If I understood your question clearly). I have assumed that you don't need to show anything regards empty textboxes.
Keashan

Resources