How to get rid of prompt on quitting MS ACCESS - vbscript

I am running a Sub procedure stored in an ACCESS database using VBScript. The procedure queries the database and exports/saves a CSV file.
My problem is that the script successfully opens the database, runs the procedure but then leaves ACCESS open because ACCESS opens a prompt asking "Are you sure that you want to leave ACCESS" (rather its equivalent in German). I want it to close without interaction.
The basic idea of this script is to run it via the Windows Task Scheduler. (Which does not work right now but that is another question.)
Here is the part of my VBScript dealing with the ACCESS database:
Dim objAccess
Set objAccess = createObject("Access.Application")
objAccess.OpenCurrentDataBase("C:\FiselTools\Allgemein\Scripte\Pellenc-CopyBelegarchiv3.accdb")
objAccess.Run "CopyBelegarchiv"
objAccess.Quit acQuitSaveAll
Set objAccess = Nothing
Using this script manually, it does open the database, export the file, finally executes the part following the code from above - so only closing ACCESS does not work and my guess is that it's not a problem with the script or the procedure but the ACCESS.

Erik A basically gave the right answer: there was some VBA code that is responsible for this prompt (see below). It was part of the primary form.
So in this case I found two possible solutions:
Do not display/show the form (which worked for me as I made a copy of the file dedicated for just this purpose).
If you need the form then you might need to delete the VBA code.
Private Sub Form_Unload(Cancel As Integer)
Dim Answer As Variant
Answer = MsgBox("Wollen Sie die Anwendung wirklich beenden?" & vbNewLine & vbNewLine & "Nicht gespeicherte Änderungen gehen dabei möglicherweise verloren", vbQuestion + vbYesNo + vbDefaultButton2)
If Answer = vbNo Then
Cancel = True
Else
DoCmd.Quit acQuitSaveNone
End If
End Sub
Since it is part of the Access class object of the form that is configured to be displayed on opening the ACCESS database and I do not need the form when I run the procedure via VBScript I simply changed the database to not display any form when the database is opened.
Some day I will remove any form, query, and any other object not needed from this special copy of the database that I don't need to run from VBScript.
Thanks!

Related

Get list of ALM project AND domains names in VBScript (QC11 OTA)

I am trying to list QC11 project and domain name in combo box on form load() but I am getting error object required,code I am using:
Dim tdc As New TDAPIOLELib.TDConnection
Dim projectList As Customization
Dim Project As Customization
Dim Domain As Customization
Set tdc = CreateObject("TDApiOle80.TDConnection")
tdc.InitConnectionEx "https://xyz/omu"
For Each Domain In TheTDConnection.DomainsList
Set projectList = tdc.GetAllVisibleProjectDescriptors
For Each Project In projectList
ComboBox1.AddItem (Project.Name)
ComboBox2.AddItem (Project.DomainName)
Next Project
Next Domain
If that's really the code you are using, then for a start this line is probably generating an error:
For Each Domain In TheTDConnection.DomainsList
Based on the rest of your code "TheTDConnection" should be "tdc":
For Each Domain In tdc.DomainsList
Oh, and to be doing this you should almost certainly be logged in first by calling tdc.Login... rather than just connected to the server.
On a related note, the DomainsList property is deprecated. I think you can just loop through the List of ProjectDescriptor objects returned by GetAllVisibleProjectDescriptors since that covers all projects under all domains that the current logged on user has access to.
Edit: this is a complete solution based on the original question. Here's working tested code that will cycle through the domains/projects that the provided user has access to. This assumes you have the QC/ALM Connectivity add-in installed (required).
If you are running this piece of VBScript on a 64 bit machine you need to run it using the 32bit version of wscript.exe: C:\Windows\SysWOW64\wscript.exe "c:\somewhere\myscript.vbs"
msgbox "Creating connection object"
Dim tdc
Set tdc = CreateObject("TDApiOle80.TDConnection")
msgbox "Connecting to QC/ALM"
tdc.InitConnectionEx "http://<yourServer>/qcbin/"
msgbox "Logging in"
tdc.Login "<username>", "<password>"
Dim projDesc
msgbox "Getting project descriptors"
Set projectDescriptors = tdc.GetAllVisibleProjectDescriptors
For Each desc In projectDescriptors
msgbox desc.DomainName & "\" & desc.Name
Next
msgbox "Logging out"
tdc.Logout
msgbox "Disconnecting"
tdc.Disconnect
msgbox "Releasing connection"
tdc.ReleaseConnection
Edit 2:
If you want to parse the resulting XML from sa.GetAllDomains into a list of ALL domain\project items on the server you can do this (This is VBScript since the original question & tag still mention it, and has been tested):
Set objDoc = CreateObject("MSXML.DOMDocument")
objDoc.Load "C:\yourXmlFile.xml"
Set objRoot = objDoc.documentElement
For Each domain in objRoot.selectNodes("TDXItem")
For Each project in domain.selectNodes("PROJECTS_LIST/TDXItem")
msgbox domain.selectSingleNode("DOMAIN_NAME").text & "\" & project.selectSingleNode("PROJECT_NAME").text
Next
Next

Creating a view and agent in multiple databases

The problem I am encountering is that some of the messages are not accessible by the user ID file, I would like to skip these files instead of the agent crashing out. The error message received is as follows:
Using the view approach if this happened I was able to delete the document temporarily and re-run the agent but if there is a way to skip documents it would be a great help.
Thanks for the help guys.
Ok I have amended the code to a point where I am almost comfortable with it.
Sub Initialize
Dim s As New notessession
Dim db As notesdatabase
Dim view As notesview
Dim doc As notesdocument
Dim nextdoc As notesdocument
Set db = s.currentdatabase
If view Is Nothing Then
Set view = db.CreateView("Encrypted",{Encrypt="1"})
End If
Set doc = view.getfirstdocument
On Error Goto ErrorHandler
While Not doc Is Nothing
nextDocument:
Set nextdoc = view.getnextdocument(doc)
'The below loop is mandatory to ensure that all $File entries are unecrypted
Forall i In doc.items
If i.isencrypted Then
i.isencrypted=False
End If
End Forall
'Must have at least 1 field encrypted in order to call Encrypt method
Dim temp As New NotesItem(doc,"tempjunk","temp")
temp.IsEncrypted=True
Call doc.encrypt
Call doc.save(True, False)
'This portion can now remove the fields relative to encrypting the
'single token encrypted field.
Call doc.removeitem("$Seal")
Call doc.removeitem("$SealData")
Call doc.removeitem("SecretEncryptionKeys")
Call doc.removeitem("Encrypt")
Call doc.removeItem("tempjunk")
Call doc.save(True, False)
Set doc = nextdoc
Wend
Exit Sub
ErrorHandler:
On Error Resume nextDocument
Exit Sub
End Sub
The error handling is not playing nice;
On Error Resume nextDocument is showing up as an error.
I have tried suppressing all of the error warnings which seems to attempt to strip the encryption but I think they body of the messages is being destroyed as a result.
It is no problem to create an agent in a container database and let that agent access documents in all "target" databases and modify them accordingly - No need to copy that agent to all databases.
Only restriction: If the databases are on another server, then on the server security tab of the target server you have to enter the server with the container database as trusted server.
AND: If your agent runs longer than the allowed maximum run time for agents on the server, then it will be killed prematurely.
There is no need to create views in the target databases, you can use NotesDatabase.Search() to get the corresponding documents in the databases...
You can create views by copying them from another database. Say you create a view "Encrypted" in your db with the agent.
Then add a piece of code to get a handle of this view as a NotesDocument:
Dim dbThis As NotesDatabase
Dim viewTemplate As NotesView
Dim docView As NotesDocument
Set dbThis = s.currentDatabase
Set viewTemplate = dbThis.getView("Encrypted")
Set docView = dbThis.Getdocumentbyunid(viewTemplate.Universalid)
In the agent loop, test if view Encrypted exists, if not copy the "view template":
Set view = db.getview("Encrypted")
If view Is Nothing Then
Call docView.Copytodatabase(db)
Set view = db.getview("Encrypted")
End If
Finally, if you insist, a similar procedure might be used to copy the agent to all databases, but for me the idea of running the agent in one db sounds better.
Edited: In the view of full disclosure - of course you can create a view (I guess that was the original question).
If view Is Nothing Then
Set view = db.Createview("Encrypted", {Encrypt="1"})
End If
Or do one-shot dbSearch suggested by Torsten, with a good re-mark of Richard - if you intend to run your code several times - say if encrypted documents might get created again or re-encrypted, rather go for the view.
My method is a bit old fashioned (pre-dates availability of createView) and works well if you need more than selection formula, so you can pre-build a complicated view for re-use.
Performance-wise: whatever method you will choose either creating view using createView or copying from other db or doing dbSearch there is going to be a certain slow-down while the view gets built or dbSearch executes. Karl-Henry's approach will avoid this search/view build, but will be relatively slow if there are not many encrypted documents.
Whichever method you choose - here is a small tip to boost performance. Make your loops like this to release memory as you go; for example, assuming Karl-Henry's approach:
Dim doc1 as NotesDocument
Set doc = col.GetFirstDocument()
Do Until doc Is Nothing
Set doc1 = col.GetNextDocument(doc)
formname = doc.GetItemValue("Form")(0)
If IsElement(exclude(formname))=False Then
Call RemoveEncryption(doc) '*** Your function to remove encryption
End If
' releasing memory when processing thousands of documents improves performance and avoids crashes
Delete doc
Set doc = doc1
Loop
Now again, as you are talking only about migration (so one shot) of 20+ databases, the speed or implementation details should not be that critical.
If you have to process all (or almost all) documents in each database, you can use db.AllDocuments. It is more efficient than using db.Search() with an #All formula.
If you want to exclude certain documents, perhaps based on the form name, I would build a list of forms to exclude, and then use IsElement to check each document being processed against that list.
Dim exclude List As Boolean
exclude("FormA")=True
exclude("FormB")=True
Set col = db.AllDocuments
Set doc = col.GetFirstDocument()
Do Until doc Is Nothing
formname = doc.GetItemValue("Form")(0)
If IsElement(exclude(formname))=False Then
Call RemoveEncryption(doc) '*** Your function to remove encryption
End If
Set doc = col.GetNextDocument(doc)
Loop
Something like that. By the way, you can create the list as any data type. I just choose Boolean as it is a small data type, and that it makes the code easier to read. The IsElement() function just check if the element exists, it does not use the value you set.
You would wrap the code above in a function and call it once per database.
Appended answer, based on additional info in original question:
That should not be hard, just add error handling to your code.
Before you start to loop throung the document:
On Error Goto errHandler
Before you get the next document in the loop:
nextDocument:
At the end of your code:
Exit Sub
errHandler:
Resume nextDocument
End Sub
Try that.

I cannot obtain Metadata using ADO/ADOX

I am trying to obtain meta data based on a users selection. I am using the ADODB namespace to provide a connection to the database and a recordset to retrieve data from it. I have set up a connection and tested it, this works fine, but the retrieval of the data is not working.
Here is the main segment:
con.Open()
cat.ActiveConnection = con
Select Case chk.Tag
Case "Yes"
For Each modMainFunctions.tbl In cat.Tables
If tbl.Type = "TABLE" Then
frmMain.lstTables.Items.Add(tbl.Name)
End If
Next
End Select
Essentially, I am checking if a particular checkbox has been selected, if it is has "i.e. case "yes" then I am trying to retrieve the Database TABLES from the provided database. However, the compiler doesn't reach the FOR loop and I cannot understand why...
modMainfunctions is my module with the main functions of my program are stored, within it I declare all my necessary variables:
Dim dbname As String = ""
Dim dblocation As String = Application.StartupPath
Dim con As New ADODB.Connection
Dim cat As New ADOX.Catalog
Dim tbl As New ADOX.Table
Dim col As ADOX.Column
Dim view As ADOX.View
Dim key As ADOX.Key
Dim index As New ADOX.Index
Can anyone see where I am going wrong? I want to obtain the meta data about a database (Please do not answer with "you need to connect to..." etc because I have already a subroutine which deals with this and it's working fine, I don't think it is a connection issue)
Problem solved, rather than storing this procedure in a module I simply stored it in the active form, I am unsure if this is the "best practise" but so long as my code is working as expected I'm happy. I realised that the error was because I didn't spell the world "yes" correctly... It was looking for the word "yes" and I typed "Yes" clearly the significance of thoroughly reading your code can be learnt here!

ms access linked image relitive path

I have an Image object.
I have the Picture type set to linked, so I can change the picture if I want.
I have the Picture property set to the picture name.
I would think that access would use relitive addressing and simple looking in the current directory for the image. But it does not and I get an error telling me it cannot find the picture.
Anyone have a solution? (Other than setting the Picture type to embedded or using the full file address?)
Thanks!
Update:
Tried this:
Private Sub Form_Load()
Dim file As String
file = CurrentDb().Name
file = Replace(file, ".mdb", ".bmp")
Me.Image46.Picture = file
End Sub
It works, except I still get the error message. I click O.K. and it works. Just need the error message to go away.
SOLUTION: Use the above code (or the code posted in the answer below) and then set the 'picture type' to "embedded" and then delete the 'picture' field so that it says "(none)".
Save and run.
It should work.
THANKS!
You could set the property on the forms OnLoad event like this
Me.imgMy_image.picture=getDBPath & “mypicture.bmp”
Here is the getDBPath function
Public Function GetDBPath() As String
Dim strFullPath As String
Dim I As Integer
strFullPath = CurrentDb().Name
For I = Len(strFullPath) To 1 Step -1
If Mid(strFullPath, I, 1) = "\" Then
GetDBPath = Left(strFullPath, I)
Exit For
End If
Next
End Function
Before anyone comments yes I know in access 2000 and above you can use currentproject.path but I’m stuck in the land that time forgot so need that custom function, it still works with later versions of access
Current folder depends of the way you open database in Access. At least, if you open it thru "File-Open", current folder changes to the folder of MDB file. But if you open via double-clicking MDB in explorer, it does not.

Get current Sub name for a VB-script

I am developing VBscript for GUI testing. And I wonder if there is possibilites to get the current Sub name.
I have divied the GUI testing into different Sub and want to log the Sub name to the logg file to track what is run.
So this i that I want
Sub TestCase1
Log.Message(SubName)
' Rest of test
End Sub
By using this I don't have the sub name hardcoded as a text string
VBScript (unlike JScript) doesn't provide any means to get the current routine name.
What you need is to run your script routines as test items (you're using TestComplete, right?) — in this case you'll get a tree-like log with messages grouped by script routines.
You'll also be able to access the test items programmatically via the Project.TestItems object. For example, you'll be able to get the name of the current script routine that is run as a test item using the Project.TestItems.Current.ElementToBeRun.Caption property.

Resources