I am attempting to use a CommandButton to populate TextBoxes in a Userform based on the entries in a ListBox and can't find a way to make it work.
The below code works to populate the first TextBox (TextBox2), but everything below is coming in blank. You'll notice I added & "" after TextBox3-11 - that's because I was receiving an "Invalid Use of Null" error.
How do I get each item in the ListBox to populate in it's corresponding TextBox?
Private Sub CommandButton5_Click()
Dim i As Long
i = ListBox1.ListIndex
With Me.ListBox1
TextBox2.Text = .List(i, 0)
TextBox3.Text = .List(i, 1) & ""
TextBox4.Text = .List(i, 2) & ""
TextBox5.Text = .List(i, 3) & ""
TextBox6.Text = .List(i, 4) & ""
TextBox7.Text = .List(i, 5) & ""
TextBox8.Text = .List(i, 6) & ""
TextBox9.Text = .List(i, 7) & ""
TextBox10.Text = .List(i, 8) & ""
TextBox11.Text = .List(i, 9) & ""
End With
End Sub
Related
I need to move this search to a userform that currently performs a search on sheet 1 I would like to put the database on sheet 2, everything works but the insert function cannot find the last row.
Private Sub ComboBox1_Change()
Dim Riga As Long
If ComboBox1.Value <> "" Then
Riga = ComboBox1.ListIndex + 2
Cells(Riga, 1).Select
TextBox1 = ComboBox1.Value
TextBox2 = Sheets(2).Cells(ActiveCell.Row, 2)
TextBox3 = Sheets(2).Cells(ActiveCell.Row, 3)
TextBox4 = Sheets(2).Cells(ActiveCell.Row, 4)
TextBox5 = Sheets(2).Cells(ActiveCell.Row, 5)
TextBox6 = Sheets(2).Cells(ActiveCell.Row, 6)
End If
End Sub
Private Sub CommandButton_Chiudi_Click()
Unload Me
End Sub
Private Sub CommandButton_Nuovo_Click()
Dim tx As Byte
ComboBox1.Value = ""
Cells(Range("A1").End(xlDown).Row + 1, 1).Select
For tx = 1 To 6
Userform1.Controls("TextBox" & tx).Text = ""
Next tx
TextBox1.SetFocus
End Sub
Private Sub CommandButton_Cancella_Click()
Dim Messaggio As String, Stile As String, Titolo As String
Dim Risposta As VbMsgBoxResult
Titolo = "Cancellazione Record."
Messaggio = "Vuoi cancellare il tesserino:" & Chr(10) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 1) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 2) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 3) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 4) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 5) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 6) & Chr(10)
Stile = vbYesNo + vbQuestion + vbDefaultButton2
Risposta = MsgBox(Messaggio, Stile, Titolo)
If Risposta = vbNo Then End
ActiveCell.EntireRow.Delete
End
End Sub
Private Sub CommandButton_Aggiorna_Click()
Dim Riga As Long
Dim Messaggio As String, Stile As String, Titolo As String
Dim Risposta As VbMsgBoxResult
Titolo = "Cancellazione Record."
Messaggio = "Vuoi registrare il tesserino:" & Chr(10) & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 1).Value & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 2).Value & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 3).Value & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 4).Value & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 5).Value & Chr(10) _
& Sheets(2).Cells(ActiveCell.Row, 6).Value & Chr(10) & Chr(10) _
& "con:" & Chr(10) _
& TextBox1.Value & Chr(10) _
& TextBox2.Value & Chr(10) _
& TextBox3.Value & Chr(10) _
& TextBox4.Value & Chr(10) _
& TextBox5.Value & Chr(10) _
& TextBox6.Value
Stile = vbYesNo + vbQuestion + vbDefaultButton2
Risposta = MsgBox(Messaggio, Stile, Titolo)
If Risposta = vbNo Then End
Riga = ActiveCell.Row
ComboBox1.Value = ""
Sheets(2).Cells(Riga, 1) = TextBox1.Value
Sheets(2).Cells(Riga, 2) = TextBox2.Value
Sheets(2).Cells(Riga, 3) = TextBox3.Value
Sheets(2).Cells(Riga, 4) = TextBox4.Value
Sheets(2).Cells(Riga, 5) = TextBox5.Value
Sheets(2).Cells(Riga, 6) = TextBox6.Value
ComboBox1.SetFocus
End Sub
how can I do to indicate that with the insert new record function I find the last row on the database that I have moved to sheet 2? thank you
sheets(2).Cells(Range("A1").End(xlDown).Row + 1, 1).Select
it doesn't work and I go into error
There are two easy methods. Try them out use what ever works fine for your case.
Method 1:
Sheets(2).UsedRange.Rows.Count
Method 2:
Sheets(2).Cells(Rows.Count, col_no).End(xlUp).Row
Here you need to change col_no to your desired column. For the above example it would be like
Method 2:
Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
These both method will give you last data containing row you need to add one ( +1 ) to get next blank row for new data entry.
there's an error showing up after i update the data in my datagridview and after i click the row of updated data in my datagridview this error will shown up
Private Sub DataGridView2_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick
Dim i As Integer
i = DataGridView2.CurrentRow.Index
Me.txtEmployeeID.Text = DataGridView2.Item(0, i).Value
Me.txtFirstName.Text = DataGridView2.Item(1, i).Value
Me.txtMiddleName.Text = DataGridView2.Item(2, i).Value
Me.txtLastName.Text = DataGridView2.Item(3, i).Value
Me.txtGender.Text = DataGridView2.Item(4, i).Value
Me.txtContactNumber.Text = DataGridView2.Item(5, i).Value
Me.txtupAge.Text = DataGridView2.Item(6, i).Value
Me.txtupAddress.Text = DataGridView2.Item(7, i).Value
Me.Bdate.Text = DataGridView2.Item(8, i).Value
Me.txtPos.Text = DataGridView2.Item(9, i).Value
If DataGridView2.Item(10, i).Value Is Nothing Then Return
Using m As MemoryStream = New MemoryStream(CType(DataGridView2.Item(10, i).Value, Byte()))
PictureBox2.Image = CType(Image.FromStream(m).Clone(), Image)
End Using
End Sub
**EDIT**
when im updating, the image type in my database will change into 0x which is no image are saved.
this is the code of my update
cn.Close()
cn.Open()
With cmd
.Connection = cn
.CommandText = ("Update TBL_EMPLOYEE SET FirstName= '" & txtFirstName.Text & "', MiddleName='" & txtMiddleName.Text & "',LastName='" & txtLastName.Text & "', Gender='" & txtGender.Text & "',Age='" & txtupAge.Text & "' ,Address='" & txtupAddress.Text & "', Position='" & txtPos.Text & "',BirthDate='" & Bdate.Value.Date & "', [Picture]=#PID where [EmployeeID]=#EID ")
.Parameters.AddWithValue("#EID", CInt(txtEmployeeID.Text))
.Parameters.Add("PID", SqlDbType.Image).Value = ms.ToArray()
.ExecuteNonQuery()
.Dispose()
.Parameters.Clear()
txtFirstName.Text = ""
txtMiddleName.Text = ""
txtLastName.Text = ""
txtGender.Text = ""
txtContactNumber.Text = ""
txtupAge.Text = ""
txtupAddress.Text = ""
txtPos.Text = ""
Bdate.Text = ""
PictureBox2.Image = Nothing
MsgBox("Employee Updated", vbInformation, "Information Message")
datagridshow()
End With
End If
when i update this image the image will not save. there's wrong syntax in my update
im missing this code : PictureBox2.Image.Save(ms, PictureBox2.Image.RawFormat) my bad. :'(
If you're sure DataGridView2.Item(10, i).Value has a value here then probably the MemoryStream is being Disposed too quickly. I'd suggest trying:
m As MemoryStream = New MemoryStream(CType(DataGridView2.Item(10, i).Value, Byte()))
PictureBox2.Image = Image.FromStream(m)
without Using
Here's my entire code below. I keep getting a Run-Time Error 13, any help would be appreciated.
Private Sub Button_Clear_Click()
Input_Wager.Text = "0"
End Sub
The section below, I would like when the command button is pressed that if the Textbox Input_Wager is empty, it displays a message, otherwise it starts the computations in the elseif section
Private Sub Button_Submit_Click()
If Me.Input_Wager.Text = "" Then
MsgBox ("Display")
ElseIf Me.Input_Wager.Text <> "" Then
TextBox2.Text = "$" & (Me.Input_Wager * 35)
TextBox3.Text = "$" & (Me.Input_Wager * 17)
TextBox4.Text = "$" & (Me.Input_Wager * 8)
TextBox5.Text = "$" & (Me.Input_Wager * 11)
TextBox6.Text = "$" & (Me.Input_Wager * 6)
TextBox7.Text = "$" & (Me.Input_Wager * 5)
TextBox8.Text = "$" & (Me.Input_Wager * 11)
TextBox9.Text = "$" & (Me.Input_Wager * 17)
TextBox10.Text = "$" & (Me.Input_Wager * 2)
TextBox11.Text = "$" & (Me.Input_Wager * 2)
TextBox12.Text = "$" & (Me.Input_Wager * 1)
TextBox13.Text = "$" & (Me.Input_Wager * 1)
TextBox14.Text = "$" & (Me.Input_Wager * 1)
TextBox15.Text = "$" & (Me.Input_Wager * 1)
TextBox16.Text = "$" & (Me.Input_Wager * 1)
TextBox17.Text = "$" & (Me.Input_Wager * 1)
End If
End Sub
As #Andrew and #Raymond said, you are getting a type mismatch error because you are trying to multiply a control/object with an Integer.
First check to see if the value of Input_Wager is an Integer and not a string - do this by not letting it accept anything else than an integer -
Private Sub Input_Wager_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Input_Wager.KeyPress
If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then
e.KeyChar = ""
End If
End Sub
Now that you know it has an integer value, change your multiplication lines to this -
TextBox2.Text = "$" & (Me.Input_Wager.Text * 35)
I am trying to update data from datagrid view back to the SQL server, and i get an error
" Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index "
My Code is as bellow , any help would be appreciated
Try
SQLConnection = New MySqlConnection()
SQLConnection.ConnectionString = connstr
SQLConnection.Open()
Console.WriteLine("MySQL version : {0}", SQLConnection.ServerVersion)
SQL = ""
For J = 0 To DataGridView1.Rows.Count - 1
SQL = SQL & " UPDATE `ntechtbs`.`a2sbkgsys` SET `NumberOfPax`='" & DataGridView1.Item(J, 1).Value & "', `PAXName`='" & DataGridView1.Item(J, 2).Value & "',`Origin`='" &
DataGridView1.Item(J, 3).Value & "',`Destination`='" & DataGridView1.Item(J, 4).Value & "', `Services`='" & DataGridView1.Item(J, 5).Value & "',`ArrivalDate`='" & DataGridView1.Item(J, 6).Value & "',`DepartureDate`='" & DataGridView1.Item(J, 7).Value & "',`BookingReference`='" & DataGridView1.Item(J, 8).Value & "',`Status`='" & DataGridView1.Item(J, 9).Value & "',`PriceQuote`='" & DataGridView1.Item(J, 10).Value & "',`AgencyPin`='" & DataGridView1.Item(J, 11).Value & "',`AgentPin`='" & DataGridView1.Item(J, 12).Value & "',`SIPCODE`='" & DataGridView1.Item(J, 13).Value & _
"WHERE ID=" & DataGridView1.Item(J, 0).Value & ";"
Next
MsgBox(SQL)
Dim cmd As MySqlCommand = New MySqlCommand(SQL, SQLConnection)
Dim i As Integer = cmd.ExecuteNonQuery()
If (i > 0) Then
MsgBox("Record is Successfully Updated")
Else
MsgBox("Record is not Updated")
End If
SQLConnection.Close()
I have the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ' calculate button
If String.IsNullOrEmpty(TextBox1.Text) Then
MsgBox("Please enter a value to convert!")
End If
If currentIndex = vbNull Then
MsgBox("Please select a conversion!")
End If
Select Case currentIndex
Case 1
Label2.Text = TextBox1.Text & " Celsius = " & Math.Round(((TextBox1.Text * 1.8) + 32), 2) & " Fahrenheit"
Case 2
Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((TextBox1.Text - 32) / 1.8), 2) & " Celsius"
Case 3
Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((TextBox1.Text - 273.15), 2) & " Celsius"
Case 4
Label2.Text = TextBox1.Text & " Celius = " & Math.Round((TextBox1.Text + 273.15), 2) & " Kelvin"
Case 5
Label2.Text = TextBox1.Text & " Kelvin = " & Math.Round((((TextBox1.Text - 273.15) * 1.8) + 32), 2) & " Fahrenheit"
Case 6
Label2.Text = TextBox1.Text & " Fahrenheit = " & Math.Round(((((TextBox1.Text - 32) * 5) / 9) + 273.15), 2) & " Kelvin"
Case 8
Label2.Text = TextBox1.Text & " Miles P/H = " & Math.Round((TextBox1.Text * 1.609344), 2) & " Kilometers P/H"
Case 9
Label2.Text = TextBox1.Text & " Kilometers P/H = " & Math.Round((TextBox1.Text / 1.609344), 2) & " Miles P/H"
Case 11
Label2.Text = TextBox1.Text & " Kilograms = " & Math.Round((TextBox1.Text * 2.20462), 2) & " Pounds"
Case 12
Label2.Text = TextBox1.Text & " Pounds = " & Math.Round((TextBox1.Text / 2.20462), 2) & " Kilograms"
Case 14
Label2.Text = TextBox1.Text & " Meters = " & Math.Round((TextBox1.Text * 3.2808399), 2) & " Feet"
Case 15
Label2.Text = TextBox1.Text & " Feet = " & Math.Round((TextBox1.Text / 3.2808399), 2) & " Meters"
End Select
End Sub
As you can see, I have a variable (currentIndex) and have a select case statement checking them against the various conversions, however my problem lies in the piece of code above this.
If currentIndex = vbNull Then
MsgBox("Please select a conversion!")
End If
I require it to spit an error message out if the index is null, however I cannot work out a way to do this. 0 cannot be used as this is the first entry in the index, and vbNull etc do not seem to work. Can anybody point me in the right direction? Thanks.
EDIT:
This is how current index is created:
Dim currentIndex As Integer
and this is how it is assigned:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
' Get the currently selected index of the item in the list box.
currentIndex = ListBox1.FindString(currentItem)
End Sub
I would suggest using SelectedIndex instead.
In your code:
If ListBox1.SelectedIndex = -1 Then
MsgBox("Please select a conversion!")
End If
Here is the reference for the property: http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx
If currentIndex is an index from dropdown box then value of -1 means that nothing was selected.