How to print an access report protected by password? - vb.net

I have an password-protected Access database, and I need to print a report in this database from my VB.Net code. How can I do it? I have this so far:
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAfdrukken.Click
Dim moApp As Access.Application
Dim acViewPreview As Access.AcView
Try
moApp = DirectCast(GetObject(, "Access.Application"), Access.Application)
Catch ex As Exception
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject("Access.Application"), Access.Application)
Else
MessageBox.Show(ex.Message, "VB/Office Guru™ Access Demo", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End Try
moApp.Visible = True
moApp.OpenCurrentDatabase("D:\media\urine (1).ACCDB")
moApp.RunCommand(Access.AcCommand.acCmdAppMaximize)
moApp.DoCmd.Maximize()
moApp.DoCmd.SetWarnings(False)
moApp.DoCmd.OpenReport("report1bw", acViewPreview)
moApp.CloseCurrentDatabase()
End Sub

Just use the password when opening the file.
moApp.OpenCurrentDatabase("D:\media\urine (1).ACCDB", bstrPassword := "somepassword")

Related

Auto update issue with drive C:/ in Vb.net

I've created a program with Vb.net that includes an "auto-update" feature. I made it using File A Sync and it seems to work fine, But that's only when I set up the program in any drive except the "C:/" drive, But when I set up the program on the "C:/" drive, The download doesn't start at all. I do not know why. I also tried searching for this issue online but couldn't find any help.
Imports System.IO
Imports System.ComponentModel
Public Class Updater
Dim client As New WebClient
Private Sub Updater_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Downloading_Bar.EditValue = 0
Speed_lb.Visible = True
Speed_lb.Text = " 0 kb/s"
Size_lb.Visible = True
Size_lb.Text = " 0 MB's / 0 MB's"
Try
AddHandler client.DownloadProgressChanged, AddressOf Client_ProgressChanged
AddHandler client.DownloadFileCompleted, AddressOf Client_DownloadCompleted
client.DownloadFileAsync(New Uri(Program_URL), Application.StartupPath & "\Changer NewVersion.exe")
Downloading_Bar.Properties.Step = 1
Downloading_Bar.Properties.PercentView = True
Downloading_Bar.Properties.Maximum = 100
Downloading_Bar.Properties.Minimum = 0
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub Client_ProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
Try
Speed_lb.Text = String.Format("{0} kb/s", (e.BytesReceived / 1024.0).ToString("00"))
Downloading_Bar.EditValue = e.ProgressPercentage.ToString()
Downloading_Bar.PerformStep()
Downloading_Bar.Update()
Size_lb.Text = String.Format("{0} MB's / {1} MB's", (e.BytesReceived / 1024.0 / 1024.0).ToString("0.00"), (e.TotalBytesToReceive / 1024.0 / 1024.0).ToString("0.00"))
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
client.CancelAsync()
client.Dispose()
End Try
End Sub
Private Sub Client_DownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
If Downloading_Bar.EditValue = 100 Then
Try
My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Changer.exe", "Changer OlderVersion.exe")
My.Computer.FileSystem.RenameFile(Application.StartupPath & "\Changer NewVersion.exe", "Changer.exe")
Process.Start(Application.ExecutablePath)
Process.GetCurrentProcess.Kill()
Me.Dispose()
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
Private Sub Updater_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
client.CancelAsync()
client.Dispose()
End Sub
End Class

How to send SMS with VB.NET?

So I've been trying to send sms's with my VB.NET application but I had no luck.
I add the carrier and nothing.
#Region "Methods"
Private Sub frmMain_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' set up the carriers list - this is a fair list,
' you may wish to research the topic and add others,
' it took a while to generate this list...
cboCarrier.Items.Add("#itelemigcelular.com.br")
cboCarrier.Items.Add("#message.alltel.com")
cboCarrier.Items.Add("#message.pioneerenidcellular.com")
cboCarrier.Items.Add("#messaging.cellone-sf.com")
cboCarrier.Items.Add("#messaging.centurytel.net")
cboCarrier.Items.Add("#messaging.sprintpcs.com")
cboCarrier.Items.Add("#mobile.att.net")
cboCarrier.Items.Add("#mobile.cell1se.com")
cboCarrier.Items.Add("#mobile.celloneusa.com")
cboCarrier.Items.Add("#mobile.dobson.net")
cboCarrier.Items.Add("#mobile.mycingular.com")
cboCarrier.Items.Add("#mobile.mycingular.net")
cboCarrier.Items.Add("#mobile.surewest.com")
cboCarrier.Items.Add("#msg.acsalaska.com")
cboCarrier.Items.Add("#msg.clearnet.com")
cboCarrier.Items.Add("#msg.mactel.com")
cboCarrier.Items.Add("#msg.myvzw.com")
cboCarrier.Items.Add("#msg.telus.com")
cboCarrier.Items.Add("#mycellular.com")
cboCarrier.Items.Add("#mycingular.com")
cboCarrier.Items.Add("#mycingular.net")
cboCarrier.Items.Add("#mycingular.textmsg.com")
cboCarrier.Items.Add("#o2.net.br")
cboCarrier.Items.Add("#ondefor.com")
cboCarrier.Items.Add("#pcs.rogers.com")
cboCarrier.Items.Add("#personal-net.com.ar")
cboCarrier.Items.Add("#personal.net.py")
cboCarrier.Items.Add("#portafree.com")
cboCarrier.Items.Add("#qwest.com")
cboCarrier.Items.Add("#qwestmp.com")
cboCarrier.Items.Add("#sbcemail.com")
cboCarrier.Items.Add("#sms.bluecell.com")
cboCarrier.Items.Add("#sms.cwjamaica.com")
cboCarrier.Items.Add("#sms.edgewireless.com")
cboCarrier.Items.Add("#sms.hickorytech.com")
cboCarrier.Items.Add("#sms.net.nz")
cboCarrier.Items.Add("#sms.pscel.com")
cboCarrier.Items.Add("#smsc.vzpacifica.net")
cboCarrier.Items.Add("#speedmemo.com")
cboCarrier.Items.Add("#suncom1.com")
cboCarrier.Items.Add("#sungram.com")
cboCarrier.Items.Add("#telesurf.com.py")
cboCarrier.Items.Add("#teletexto.rcp.net.pe")
cboCarrier.Items.Add("#text.houstoncellular.net")
cboCarrier.Items.Add("#text.telus.com")
cboCarrier.Items.Add("#timnet.com")
cboCarrier.Items.Add("#timnet.com.br")
cboCarrier.Items.Add("#tms.suncom.com")
cboCarrier.Items.Add("#tmomail.net")
cboCarrier.Items.Add("#tsttmobile.co.tt")
cboCarrier.Items.Add("#txt.bellmobility.ca")
cboCarrier.Items.Add("#typetalk.ruralcellular.com")
cboCarrier.Items.Add("#unistar.unifon.com.ar")
cboCarrier.Items.Add("#uscc.textmsg.com")
cboCarrier.Items.Add("#voicestream.net")
cboCarrier.Items.Add("#vtext.com")
cboCarrier.Items.Add("#wireless.bellsouth.com")
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSend.Click
' Collect user input from the form and stow content into
' the objects member variables
mTo = Trim(txtPhoneNumber.Text) & _
Trim(cboCarrier.SelectedItem.ToString())
mFrom = Trim(txtSender.Text)
mSubject = Trim(txtSubject.Text)
mMailServer = Trim(txtMailServer.Text)
mMsg = Trim(txtMessage.Text)
' Within a try catch, format and send the message to
' the recipient. Catch and handle any errors.
Try
Dim message As New MailMessage(mFrom, mTo, mSubject, mMsg)
Dim mySmtpClient As New SmtpClient(mMailServer)
mySmtpClient.UseDefaultCredentials = True
mySmtpClient.Send(message)
MessageBox.Show("The mail message has been sent to " & _
message.To.ToString(), "Mail", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch ex As FormatException
MessageBox.Show(ex.StackTrace, ex.Message, _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Catch ex As SmtpException
MessageBox.Show(ex.StackTrace, ex.Message, _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Catch ex As Exception
MessageBox.Show(ex.StackTrace, ex.Message, _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
' Upon user’s request, close the application
Application.Exit()
End Sub
#End Region
If you are trying to SMS-enable a VB.NET application, I'd use this third party product:
Esendex
I used this in such a solution. The .NET SDK is devastatingly simple to understand and execute. The rates are reasonable and the support is excellent too.

Saving an array to file

I been trying to save an array to file but I can only create the text file but no text shows up in actual file. The loop doesn't work the way I want to. I need to store the Cust array into text file.
Dim car(4) As Decimal
Dim Cust(4) As String
'Declare module-level constants.
Const PER_DAY_Integer As Integer = 15
Const PER_MILE_Decimal As Decimal = 0.12D
Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
'Calculate the rental charges
Cust(0) = NameTextBox.Text
Cust(1) = AddressTextBox.Text
Cust(2) = CityTextBox.Text
Cust(3) = ZipTextBox.Text
Cust(4) = StateTextBox.Text
Try
car(0) = Decimal.Parse(BeginTextBox.Text)
Try
car(1) = Decimal.Parse(EndTextBox.Text)
Try
car(2) = Integer.Parse(DaysTextBox.Text)
'Calculate the number of miles driven and the charges
car(3) = car(1) - car(0)
car(4) = (car(3) * PER_MILE_Decimal + car(2) * PER_DAY_Integer)
'Display the results
TotalTextBox.Text = car(4).ToString("C")
MilesTextBox.Text = car(3).ToString("N1")
Catch ex As Exception
MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
DaysTextBox.Focus()
End Try
Catch ex As Exception
MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
EndTextBox.Focus()
End Try
Catch ex As Exception
MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
BeginTextBox.Focus()
End Try
End Sub
Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
'Clear the form
AddressTextBox.Clear()
CityTextBox.Clear()
StateTextBox.Clear()
ZipTextBox.Clear()
BeginTextBox.Clear()
EndTextBox.Clear()
DaysTextBox.Clear()
TotalTextBox.Clear()
MilesTextBox.Clear()
With NameTextBox
.Clear()
.Focus()
End With
End Sub
Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
'End the program
Me.Close()
End Sub
Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
'Print the form
PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
PrintForm1.Print()
End Sub
Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click
End Sub
Private Sub WriteBtn_Click(sender As Object, e As EventArgs) Handles WriteBtn.Click
Dim outFile As IO.StreamWriter
Dim int As Integer = 0
Dim count As Integer = 0
outFile = IO.File.CreateText("Cust.txt")
Do While int < count
outFile.WriteLine(Cust(1)(int))
int += 1
Loop
outFile.Close()
End Sub
End Class
You were asked where it failed when you debug and you obviously haven't even bothered to debug or you'd have seen the issue. Look at this from your code:
Dim int As Integer = 0
Dim count As Integer = 0
outFile = IO.File.CreateText("Cust.txt")
Do While int < count
outFile.WriteLine(Cust(1)(int))
int += 1
Loop
Is int ever going to be less than count?
You can replace the entire contents of that last event handler with one line:
IO.File.WriteAllLines("Cust.txt", Cust)
Of course, you should not be using file names alone but rather a file path, but that's another issue.

Drag and Drop in datagridview in vb.net

I am using two Datagridview in my code, i drag content from Me.datagridview2 and drop it on Me.datagridview1.This process is successful. But as soon as i click the cell other than the dropped content cell, the dropped content disappears. Here's my code
Private Sub DataGridView2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
Try
If Me.DataGridView2.SelectedRows.Count = 0 Then
Exit Sub
End If
Me.DataGridView2.DoDragDrop(Me.DataGridView2.SelectedRows(0), DragDropEffects.All)
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Try
Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
If Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DataGridView1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
Try
e.Effect = DragDropEffects.Copy
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Have following doubts
1. why does the dropped content disappears.
2. whenever we begin edit on datagridview, a row in automatically added below. Why it doesn't gets added when i drop content in datagridview.?
Please help me.
Actually, i just got an alternative to my own question. Here's it.
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Try
Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
If Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
dragseldet.Tables(0).Rows.Add("", r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value, 0, 0)
dragseldet.AcceptChanges()
'Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
'Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
'Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
'Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Rather than copying the contents from r row to Me.DataGridview. all cells , i am directly adding r row to my datasource named dragsaldet . And that did the trick for me.

Why isn't my vb.net code saving a row to my Access database? (Error)

I'm trying to add a 'coupling' to my database with the following code. It give me the error "Update requires a valid InsertCommand when passed DataRow collection with new rows."
Private Sub AddCoupling_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CouplingsTableAdapter.Fill(TestDatabaseDataSet.Couplings)
End Sub
Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim newCoup As DataRow = TestDatabaseDataSet.Tables("Couplings").NewRow
newCoup.Item("Type") = ComboBoxType.Text
newCoup.Item("Part Number") = TextPartNumber.Text
newCoup.Item("Description") = TextDescription.Text
newCoup.Item("Rubber Type") = ComboBoxRubberType.Text
newCoup.Item("TKN") = Integer.Parse(TextTKN.Text)
newCoup.Item("TKmax") = Integer.Parse(TextTKmax.Text)
newCoup.Item("TKW") = Integer.Parse(TextTKW.Text)
newCoup.Item("Ambient Temp") = Integer.Parse(TextTemp.Text)
newCoup.Item("PKW") = Integer.Parse(TextPKW.Text)
newCoup.Item("Max Speed") = Integer.Parse(TextSpeed.Text)
Try
TestDatabaseDataSet.Tables("Couplings").Rows.Add(newCoup)
CouplingsTableAdapter.Adapter.Update(TestDatabaseDataSet) ' ERROR HERE '
MessageBox.Show("Saved!")
Close()
Catch ex As Exception
MessageBox.Show("Failed to save to database. E:" & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub