Hello,
My aim is to export data from my SQL database into a word document. I have managed to export data into the word document from my database however if I was to go back onto the word form and type another service ID from the database and click export to word document it would export the same ID as before. However if I re-close my program and type another service ID then it will use that service ID. I believe that the problem lies with the SQL command and the command not resetting or updating every time it is exported into word. I have also ran the debugger which shows me that the ID's are changing to the correct value. Below is my code:
Public Sub Word_Click(sender As Object, e As EventArgs) Handles Word.Click
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim tbuser As String
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
tbuser = TBuserID.Text
Const wdOrientLandscape = 1
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
oDoc.PageSetup.Orientation = wdOrientLandscape
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "ITIL Services"
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara2.Range.InsertParagraphBefore()
oPara2.Range.Text = "Application Portfolio"
oPara2.Format.SpaceAfter = 5
oPara2.Range.InsertParagraphAfter()
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 7)
oTable.Range.ParagraphFormat.SpaceAfter = 6
oTable.Rows(1).Range.Font.Bold = True
oTable.Cell(1, 1).Range.Text = "ID"
oTable.Cell(1, 2).Range.Text = "Service Name"
oTable.Cell(1, 3).Range.Text = "Application Name"
oTable.Cell(1, 4).Range.Text = "Application Type"
oTable.Cell(1, 5).Range.Text = "Cost"
oTable.Cell(1, 6).Range.Text = "Year Released"
oTable.Cell(1, 7).Range.Text = "Version"
oTable.Cell(2, 1).Range.Text = getdatatest(tbuser, 1)
oTable.Cell(2, 2).Range.Text = getdatatest(tbuser, 2)
oTable.Cell(2, 3).Range.Text = getdatatest(tbuser, 3)
oTable.Cell(2, 4).Range.Text = getdatatest(tbuser, 4)
oTable.Cell(2, 5).Range.Text = getdatatest(tbuser, 5)
oTable.Cell(2, 6).Range.Text = getdatatest(tbuser, 6)
oTable.Cell(2, 7).Range.Text = getdatatest(tbuser, 7)
oTable.Rows.Item(1).Range.Font.Italic = True
closecon()
End Sub
End Class
I have also used a function which holds the dataset.
Function getdatatest(ByVal recordnum As Integer, ByVal fieldnum As Integer)
Dim recordnum2 As String
recordnum2 = Wordexport.TBuserID.Text
Dim dbtable
dbtable = "application_portfolio"
Dim sql As MySqlCommand
sql = New MySqlCommand("Select * from application_portfolio where service_name = '" & Wordexport.tbservicename.Text & "' ", dbcon)
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
DataAdapter1.SelectCommand = sql
DataAdapter1.Fill(ds, dbtable)
dbcon.Close()
Return ds.Tables(dbtable).rows(recordnum - recordnum2).Item(fieldnum - 1)
End Function
Thank you in advance for your help.
My aim is to export data from my SQL database into a word document. I have managed to export data into the word document from my database however if I was to go back onto the word form and type another service ID from the database and click export to word document it would export the same ID as before. However if I re-close my program and type another service ID then it will use that service ID. I believe that the problem lies with the SQL command and the command not resetting or updating every time it is exported into word. I have also ran the debugger which shows me that the ID's are changing to the correct value. Below is my code:
Public Sub Word_Click(sender As Object, e As EventArgs) Handles Word.Click
Dim oPara1 As Word.Paragraph, oPara2 As Word.Paragraph
Dim tbuser As String
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
tbuser = TBuserID.Text
Const wdOrientLandscape = 1
oWord = CreateObject("Word.Application")
oWord.Visible = True
oDoc = oWord.Documents.Add
oDoc.PageSetup.Orientation = wdOrientLandscape
oPara1 = oDoc.Content.Paragraphs.Add
oPara1.Range.Text = "ITIL Services"
oPara1.Format.SpaceAfter = 24 '24 pt spacing after paragraph.
oPara1.Range.InsertParagraphAfter()
oPara2 = oDoc.Content.Paragraphs.Add(oDoc.Bookmarks.Item("\endofdoc").Range)
oPara2.Range.InsertParagraphBefore()
oPara2.Range.Text = "Application Portfolio"
oPara2.Format.SpaceAfter = 5
oPara2.Range.InsertParagraphAfter()
oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 3, 7)
oTable.Range.ParagraphFormat.SpaceAfter = 6
oTable.Rows(1).Range.Font.Bold = True
oTable.Cell(1, 1).Range.Text = "ID"
oTable.Cell(1, 2).Range.Text = "Service Name"
oTable.Cell(1, 3).Range.Text = "Application Name"
oTable.Cell(1, 4).Range.Text = "Application Type"
oTable.Cell(1, 5).Range.Text = "Cost"
oTable.Cell(1, 6).Range.Text = "Year Released"
oTable.Cell(1, 7).Range.Text = "Version"
oTable.Cell(2, 1).Range.Text = getdatatest(tbuser, 1)
oTable.Cell(2, 2).Range.Text = getdatatest(tbuser, 2)
oTable.Cell(2, 3).Range.Text = getdatatest(tbuser, 3)
oTable.Cell(2, 4).Range.Text = getdatatest(tbuser, 4)
oTable.Cell(2, 5).Range.Text = getdatatest(tbuser, 5)
oTable.Cell(2, 6).Range.Text = getdatatest(tbuser, 6)
oTable.Cell(2, 7).Range.Text = getdatatest(tbuser, 7)
oTable.Rows.Item(1).Range.Font.Italic = True
closecon()
End Sub
End Class
I have also used a function which holds the dataset.
Function getdatatest(ByVal recordnum As Integer, ByVal fieldnum As Integer)
Dim recordnum2 As String
recordnum2 = Wordexport.TBuserID.Text
Dim dbtable
dbtable = "application_portfolio"
Dim sql As MySqlCommand
sql = New MySqlCommand("Select * from application_portfolio where service_name = '" & Wordexport.tbservicename.Text & "' ", dbcon)
Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
DataAdapter1.SelectCommand = sql
DataAdapter1.Fill(ds, dbtable)
dbcon.Close()
Return ds.Tables(dbtable).rows(recordnum - recordnum2).Item(fieldnum - 1)
End Function
Thank you in advance for your help.