Reputation: 139
at the moment I'm working on a system where users can input and view comments. The input part works fine, however getting it to display comments is not working.
What I am currently trying to do is for each entry in the comments table, I want the system to create a new label and then populate that label's Text with the date, user name and then the users comment.
I've attached the code I'm below, any input or guidance would be greatly appreciated.
Try
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\IMData.mdb;")
Dim cmd As New OleDbCommand
con.Open()
cmd.Connection = con
cmd.CommandText = "Select * from Comments where ID=@p1"
cmd.Prepare()
cmd.Parameters.AddWithValue("@p1", TrackNum.Text)
Dim Comments = cmd.ExecuteReader
Dim labelY = 418
With Comments
.Read()
For Each item In Comments
Dim newComment As New Label
newComment.Name = item
newComment.Left = 983
newComment.Top = labelY
newComment.Font = Font
newComment.Text = .Item("Date") + " " + .Item("User") + " : " + .Item("Comment")
newComment.Visible = True
Me.Controls.Add(newComment)
labelY += 47
Next
.Close()
End With
con.Close()
Catch
End Try
This is the working code:
Try
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\IMData.mdb;")
Dim cmd As New OleDbCommand
con.Open()
cmd.Connection = con
cmd.CommandText = "Select * from Comments where ID=@p1"
cmd.Prepare()
cmd.Parameters.AddWithValue("@p1", TrackNum.Text)
Dim Comments = cmd.ExecuteReader
With Comments
.Read()
Dim labelY = 418
For Each item In Comments
Dim newComment As New Label
newComment.Name = labelY
newComment.Top = labelY
labelY += 47
newComment.Left = 983
newComment.Font = Font
newComment.Text = item("Date") + " : " + item("User") + " : " + item("Comment")
newComment.Width = 900
newComment.Visible = True
Me.Controls.Add(newComment)
Next
.Close()
End With
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Upvotes: 1
Views: 931
Reputation: 728
Couple of errors here and because it is in a try,catch with no output on the catch you are not seeing the issue.
See comments below for corrections:
Try
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\IMData.mdb;")
Dim cmd As New OleDbCommand
con.Open()
cmd.Connection = con
cmd.CommandText = "Select * from Comments where ID=@p1"
cmd.Prepare()
cmd.Parameters.AddWithValue("@p1", TrackNum.Text)
Dim Comments = cmd.ExecuteReader
Dim labelY = 418
With Comments
.Read()
For Each item In Comments
Dim newComment As New Label
newComment.Name = item 'this needs to be a string such as item("ID") - must be unique!'
newComment.Left = 983
newComment.Top = labelY
newComment.Font = Font
newComment.Text = .Item("Date") + " " + .Item("User") + " : " + .Item("Comment")
'these 'Item's should not have the period in front of them'
newComment.Visible = True
Me.Controls.Add(newComment)
labelY += 47
Next
.Close()
End With
con.Close()
Catch
'add in something to catch the error such as console.writeline'
End Try
Upvotes: 1