Microsoft Access 2007 Lags after Print Job - sql

I am new to Microsoft Access.
I added some queries to an old and existing database (.accdb) using Microsoft Access 2007.
I created two forms in Access, and two buttons in the Switchboard.
Whenever the buttons are clicked, I load the form which was created using the SQL query through the Form Wizard.
However, now whenever I print, the particular form which has the print button using the following old existing code.
Private Sub CmdPrintInv_Click()
On Error GoTo Err_CmdPrintInv_Click
Dim stDocName As String
Dim sqlstr As String
sqlstr = "InvType='" & InvType.Value & "' AND InvNo='" & InvNo & "'"
stDocName = "R-Invoice"
DoCmd.OpenReport stDocName, acNormal, , sqlstr
Exit_CmdPrintInv_Click:
Exit Sub
Err_CmdPrintInv_Click:
MsgBox Err.Description
Resume Exit_CmdPrintInv_Click
End Sub
Previously, the database was working fine after clicking print. The other buttons were responsive.
Now, the database lags, particularly the window attached. Whenever I try to click other buttons, they are not responsive.
Any advise would help me out.

Related

Creating a form in Access to get the Customer Information

I have created a database named Customer_master in which I have some customer information saved in an MS Access database.
I am trying to create a Form so that I can enter a Mobile Number and click the Search button and get the Customer Name.
The form is created with the Mobile Number & Customer Name as text boxes and a Search button in front of the mobile number.
As I am new to VBA, I think there is some coding issue.
Below is the code I have tried for the Search button:
Private Sub Command6_Click()
Dim strsearch As String
Dim Task As String
'Check if a keyword entered or not
If IsNull(Me.Mobile_Number) Or Me.Mobile_Number = "" Then
MsgBox "Please type in your search keyword.", vbOKOnly, "Keyword Needed"
Else
strsearch = Me.Mobile_Number.Value
Task = "SELECT Customer_Name FROM Customer_Master WHERE ((Mobile_Number Like ""*" & strsearch & "*""))"
Me.RecordSource = Task
End If
End Sub
After I enter the Mobile Number and click on the Search button nothing is happening as I am not getting the name of the customer.
An alternative way to achieve this is to create a new query in access against the datasource, and in the query definition pass the value from your search form to the query, using FORMS!VALUE.
You can then add a button to your form and save the VBA code to execute the query on click. by adding in the line DoCmd.OpenQuery

How to use VBA to add new record in MS Access?

I'm using bound forms for the user to update information on new or existing customers. Right now I'm using a Add New Record macro on the submit button (because I'm not sure how to add or save a new record through VBA).
I added a before update event (using VBA) to have the user confirm they want to save changes before exiting the form. For some reason this is overriding the add record button and now users cannot add new record until exiting the forms.
How can I use VBA to add new customer information to the correct table? Is this something that should be done with macros instead?
Form BeforeUpdate Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strmsg As String
strmsg = "Data has been changed."
strmsg = strmsg & " Save this record?"
If MsgBox(strmsg, vbYesNo, "") = vbNo Then
DoCmd.RunCommand acCmdUndo
Else
End If
End Sub
Add Record Button:
Private Sub btnAddRecord_Click()
Dim tblCustomers As DAO.Recordset
Set tblCustomers = CurrentDb.OpenRecordset("SELECT * FROM [tblCustomers]")
tblCustomers.AddNew
tblCustomers![Customer_ID] = Me.txtCustomerID.Value
tblCustomers![CustomerName] = Me.txtCustomerName.Value
tblCustomers![CustomerAddressLine1] = Me.txtCustomerAddressLine1.Value
tblCustomers![City] = Me.txtCity.Value
tblCustomers![Zip] = Me.txtZip.Value
tblCustomers.Update
tblCustomers.Close
Set tblCustomers = Nothing
DoCmd.Close
End Sub
In order to submit a record using VBA, create an On Click event for the button, and in that Sub run the following command:
Private Sub submitButton_Click()
'All the code to validate user input. Prompt user to make sure they want to submit form, etc.
DoCmd.RunSQL "INSERT INTO tblCustomers (CustomerID, CustomerName, CustomerAddressLine1, City, Zip) values (txtCustomerID.Value, txtCustomerName.Value, txtCustomerAddressLine1.Value, txtCity.Value, txtZip.Value)"
End Sub
In this Sub, you can add all the code you want to validate the values that the user entered and choose whether or not you want to submit the record. There's a lot of control using VBA to submit your forms, so you do not need a BeforeUpdate event.
Also, do NOT use bound forms with this method. I don't know what the repercussions are but I wouldn't try it. Access is great for starting off, but as you want to do more complex things, it is easier to just use VBA.
It seems strange that you would create a before update event for your form to create a prompt before closing. Perhaps you should try the on close event instead. If you want to use vba to add a new record from the form you can simplify your statement. I came across a similar situation when designing my own form for my Access DB. This code is tested and working:
Dim sVIN As String
Dim sMake As String
Dim sModel As String
Dim sColor As String
Dim sType As String
Dim intYear As Integer
sVIN = Me.txtVIN.Value
sMake = Me.txtMake.Value
sModel = Me.txtModel.Value
sColor = Me.txtColor.Value
sType = Me.comboType.SelText
intYear = Me.txtVehicleYear.Value
DoCmd.RunSQL "INSERT INTO Vehicles (VIN, Make, Model, VehicleYear, Color, Type) VALUES ('" & sVIN & "', '" & sMake & "', '" & sModel & "', " & intYear & ", '" & sColor & "', '" & sType & "')"
If you are just using one DB in your project and you're connecting on start up you can sometimes run simple DoCmd.RunSQL statements like this. You can take the syntax here and adapt it to your own project. I myself got the basic syntax from W3 schools. Good site for learning to write SQL queries. It should also be noted that validation testing is not included here. You should make sure it is included in the form validation rules or in vba code. One more thing... in your SQL it looks like you are attempting to assign a value to the ID column from a text box entry; if ID is an auto number column, don't do this. ID columns are usually assigned a number automatically, so you don't need to (or want to) specify an insert value for that.
As a side note:
I noticed that adding records via VBA functionality(as in your "Add Record Button" code) works ~400 times faster than using DoCmd.Run SQL "INSERT INTO...":
~55k records/s compared to 140 records/s for a table with 5 columns(ID+4 short strings).
This has no practical meaning in terms of a form, where data is entered manually but if the form is generating more records, this saves a lot of time.

Writing to Access table fails when an Excel workbook has an open connection to it

I am trying to make a windows form app, that stores information in an access database which can then be viewed in Excel.
I have got it to write to the database, and it workes fine until I try to imort the data to Excel. I then get the error: "operation must be an updateble query"
This seems to have something to do with the database beeing imported to Excel, somehow.
Does anyone know what this is, and what can be done?
The reason I need to view the data in excel has do do with my very conservative and anti-access colleagues, who are used to it.
Here is the code i am trying:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using connection As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\espen\documents\visual studio 2012\Projects\ProjectName\database.accdb;Persist Security Info=False;")
connection.Open()
Dim sqlString As String = "INSERT INTO TableName ([column1], [column2]) VALUES('" & TextBox1.Text & "', '" & TextBox2.Text & "')"
Using command As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, connection)
command.ExecuteNonQuery()
End Using
connection.Close()
End Using
End Sub
I was able to recreate your issue. It appears to be caused by the default settings used when Excel creates a connection to an Access database. The default connection string includes the option
Mode=Share Deny Write;
so when Excel has the connection open no other process can write to the Access database.
The following fix seems to work for me:
In Excel, click the "Connections" button on the "Data" tab of the ribbon. With the appropriate connection selected in the list, click the "Properties..." button. On the "Definition" tab, edit the "Connection string" to use
Mode=Share Deny None;
as shown here:
Save the changes, then close and re-open the Excel workbook.

MS Access - "phantom" process created by canceling report

I have encountered a problem in Access 2010 for Windows:
Launch a report using doCmd.OpenReport
Set "Cancel = True" in an event associated with the report
Close Access
Access continues to run as a "phantom" process and must be killed in Task Manager. Access cannot open another database until this process is killed. The task initially shows some CPU utilization but quiets down to 0%.
I discovered this while using doCmd.OpenReport to launch a report. The report opens a form in the Report_Open event. The form prompts the user for report parameters and allows them to press "OK" to display the report or press "Cancel". The On Click event for "Cancel" sets "Cancel = True".
It appears that OpenReport() is not responding gracefully to the cancel. This seems like a frequently used technique so I hesitate to call it a bug and am wondering if I am doing something wrong. Actually... the more I explain this, it does sound like a bug. At this point, I am hoping somebody has a workaround or that I am missing something obvious.
This is just a simplified example I created to illustrate the problem:
form
Private Sub Form_Open(Cancel As Integer)
On Error GoTo errHandler
DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog
Exit Sub
errHandler:
Select Case Err.Number
Case 2501 ' Cancelled by user, or by NoData event.
MsgBox "Report cancelled, or no matching data.", vbInformation, "Information"
Case Else
MsgBox "Error " & Err & ": " & Error$, vbInformation, "Form_Open()"
End Select
Resume Next
End Sub
report
Private Sub Report_Open(Cancel As Integer)
Cancel = True
End Sub
The reason for the problem in this case is acDialog:
DoCmd.OpenReport "Test Report", acViewPreview, , , acDialog
I think you will find:
DoCmd.OpenReport "Test Report", acViewPreview
Works without problems.
Edit re Comment
You should not need to cancel the report. For the most part, it is better to avoid errors than to trap them, so check for data and get parameters before opening the form. Move the form from the report open event to step of its own and use DLookUp or a query to check for data before launching the form.
I'm not in a position to test this problem, so I'll just offer a generic solution: Remove the data/recordset/recordsource from the report definition.
You can apply the recordsource to the report after you have determined the parameters. If the report is canceled, it is canceled without ever having a data connection.

Hot to pass SQL query from Access form to external database ?

I have database created in postgreSql and working on front-end application using microsoft access forms. I want to send a query on a press of a button:
Private Sub Command5_Click()
Dim strSQL As String
strSQL = "INSERT INTO public_failedestimate (est_num,issue_date) SELECT est_num,issue_date FROM public_estimate WHERE est_num=1 ;"
DoCmd.RunSQL strSQL
End Sub
It works when debugging, but does not when i click the button. By the way, it located in button's on click event. thx
What you have should work. If there is any possibility that the current record in the form's bound record set is dirty, then you want to ensure that the record is written to the table BEFORE you run this code.
You can add these 3 lines of code right before your RunSQL command
If me.dirty = true then
Me.dirty = false
End if