Calling Access Macro from VB.NET problems - vb.net

I call an Access macro from vb.net like so:
Acc.DoCmd.RunMacro("Macro1")
The Macro in Access has many OpenQuerys and Msgbox with a message saying "data done" at the end.
When I execute the macro from vb.net, it shows the data done message and then done. However, when I analyse the table's to see if data has been appended, it hasen't.
When I run the same macro from within Access, it works fine. It does show many messages like "You are about to run an append query that will modify data in your query" and I hit yes and does take slightly longer, but it does do it.
In VB.NET, the only message I get is the final messagebox.
I have also tried:
Acc.SetOption("Confirm Action Queries", 0)
Acc.SetOption("Confirm Document Deletions", 0)
Acc.SetOption("Confirm Record Changes", 0)
before executing the macro from within VB.NET but to no avail. Still works the same.
Is there a way to fix it?
EDIT: My Access DB is mdb file

I think the problem is with the UI messages in the macro:
It does show many messages like "You are about to run an append query that will modify data in your query" and I hit yes
There's an option in Access to suppress these confirmation queries, you'll want them suppressed in the .mdb file. Looks like attempting to suppress them from VB isn't working.

Related

MS Access: Query to list number of files in a series of folders

I have folders labeled by their keyfield, so 1, 2, ... 999, 1000. located in currentproject.path\RecordFiles\KeyFieldHere so like currentproject.path\RecordFiles\917.
I want to run a query that will count how many files are in each folder. I know this can be done with the DIR function through visual basic, but I can't seem to run it through a SQL query.
I've tried using this function in a SQL equation, so Expr1: [FlrFileCount("Y:\Education\Databases\RecordFiles\")] as one of the fields just to see if it can work, but it prompts me for a value and then returns nothing.
EDIT: I tried an approach using the FlrFileCount function in a continuous form, and it does work, BUT... I get an error after every single line. I have a field in a continuous form of =FlrFileCount([currentproject].[path] & "\recordfiles\" & [ID]), but when I run the form I get an error "Error 76, Error source: FlrFileCount, Error description: Path not found." Which is crazy because IT WORKS, it properly lists the number of files in the folder for each record.
I just need to get this functionality over into a SQL query so I can pull that data for mail merges.
I currently have something similar in a form. The form has an onload property to run a module (Link here) to create a list of all the files in the relevant folder to that record, and then I have another field that just counts the number of entries in the list. However a list can't be a value in a SQL query, so I don't think that code will help.
Thanks to Tim Williams, the answer was to put
=FlrFileCount(Currentproject.Path & "\recordfiles\" & [ID])
It seems the [currentproject].[path] part was where the error was. What's confusing is that in other places, MS Access adds the extra [] around currentproject and path, and I don't know why.
Thank you so much for your help! Now to the tricky part: Implementing a proper naming scheme by program ID across a sharepoint so that the relevant folder can be opened consistently even when program names change.

Run-Time Error 1004: MS Excel is refreshing some data. Please try again later using Query

I have a dynamic query in a sheet that uses a parameter in cell C1. So, In column A there are codes, and in column B are those code descriptions and based on the value in cell C1, the query will refresh to display codes related to the value in C1. When the value in C1 changes, I get a "Run-Time Error 1004: MS Excel is refreshing some data. Please try again later", every time. When I select "End" it goes away and refreshes the query successfully, after a couple seconds, Does anyone know the reason behind this? Can I get rid of it?
There is a search feature. when the user clicks "Search" to search for the codes they're looking for, the search button is assigned a macro that essentially just navigates to the sheet which displays the codes. However, in that macro, I added this bit of code:
Sheets("department_lookup").Cells(1, 3).Value = Sheets("lookup").Cells(2, 2).Value
With Sheets("department_lookup") .Range(.Cells(1, 1), .Cells(LR, LC)).AutoFilter field:=3, Criteria1:=.Cells(1, 3).Value, VisibleDropDown:=True End With
I found this occurs when you are using a combination of Excel native queries and VBA DB code. For me, I found I couldn't access the database using an OLEDBConnection when Excel was refreshing using it's native refresh function.
SOLUTION:
I had to turn BackgroundQuery = False on the native queries.
(This can be done from the Query properties using the GUI)
EXPLANATION:
BackgroundQuery allows you to use the spreadsheet while it is refreshing data. This is okay for a user who wants to use a spreadsheet but not for a VBA macro that wants to talk to that same database. When set to false it causes Excel to hang during a refresh. This stops your operations from colliding.
If this isn't your exact scenario look for similar collisions where 2 queries are stepping upon eachother.
For people like me who knows a little bit about ODBC, from Excel, go to Data tab. Click on Queries & Connections. Then, right-click each connection tile, click Properties. Under usage, untick Enable background refresh. That should do the trick. At least, for me, that worked. :D

Is it possible to copy the contents of the Immediate window in Excel VBA?

Question
I would like to know whether it is possible to copy or extract the contents of the Immediate window in Excel VBA, so I can store it somewhere. I already know you can copy and paste from the window manually, I am interested in whether it is possible to do it with code (preferably in VBA itself).
Explanation
Questions similar to this have been asked a few times on SO (e.g. this and this), no-one has given a definitive answer and I was unable to find one from a quick search. Most of the time, answers respond asking why anyone would want to do that, and give ways to get around it in the circumstances provided in the question (e.g. how to output messages to a file or cell range). Given this, I have thought of a couple of example scenarios where someone might want to do this and cannot (easily) get around it.
Example scenarios
A) I am developing a large workbook including a large set of macros, debugging as I go. This includes using debug.print statements and directly querying in the Immediate window, e.g. ? myVar. At the end of the session, I would like to be able to automatically copy the contents of the immediate window into a log file, so I know what happened during my debug session afterwards. This could, for example, make use of the Workbook_BeforeClose event procedure.
B) I am working with two workbooks that contain VBA projects - one I can't edit, another that I can and am working on. The one I can't edit outputs debug information to the immediate window via debug.print that I would like to store somewhere (but I don't really care where).
Disclaimer
I ask this question purely out of curiosity. I suspect I already know the answer (it's not possible to do this), but I am not sure.
Yes-- but not with control-c .... select what you need and then drag and drop
1-Create a sheet named "debug.print"
2-Hide it:
Sheets("debug.print").Visible = 2 'xlSheetVeryHidden, only can be visible with vba
3-Create this function:
Function debug_print(c As String)
ThisWorkbook.Sheets("debug.print").[a1048576].End(xlUp).Offset(1).Value = c
Debug.Print c
End Function
4-Replace your codes "debug.print" to "debug_print"
5-Example:
Sub blablabla()
debug_print "i am doing whatever"
End Sub
But: Using "?" in the immediate window will not save
There is many ways to export this as file now like CSV, TXT, save in SQL etc...

Embed console in form

TL;DR:
Is there a way to embed a console inside a form, so it becomes a part of it?
Scenario:
I am writing a chat-application with client, server and database using Windows Forms in VB.NET. The server should log all communication it has with clients in a textbox.
The Problem:
So far that would not be a problem - If there wasn't a maxlength for strings! I expect this server to almost never stop (Okay, there is always some point where it does.. but lets ignore that). So if i go for Textbox1.Text &= vbnewline & "Something" it will some day reach this length and run into an exception each time something is about to be logged. I also don't want to just remove the first characters of the string.
My Idea for a solution:
My idea for a work around: Use a console instead of a simple textbox and embed it into the form, so it becomes a part of it. Is there a simple way to do that? By simple I mean that I should have to write thousands of lines of code to achieve that goal.
I am open minded for different ideas and ways to do so as well.
Why not just log your chat to file (you could create a file per day)?
Dim filename = String.Format("C:\Temp\{0:yyyy-MM-dd}-log.txt", DateTime.Now)
My.Computer.FileSystem.WriteAllText(filename, "line of text", True)
Then to display it - use a ListBox and append each new line to the end of the listbox? Each time you append, you could check how many items are in the listbox and remove the first 100 if you are over 1000 as an example.

macro to return active document format for IF/Then statement

I am creating a document template for a report for my staff to use and I have a command button at the bottom that will delete all of the command buttons in the report and protect it as read only to close out the report.
I don't want someone accidentally making these changes to the template if they happen to open that instead of a new document based on it.
So I would like a string of code that checks the active document, if it is .dotm I want it to display a message box and exit. if it is a .docx I want it to continue with the rest of the code I have written.
I have been unable to return the format or use it in an IF/THEN statement. I have been unable to find anything on the net on this either. Is it impossible? or should I be checking for the file extension? If so how would I use that as a value in an IF/THEN Statement?
The document may have been based on the template, but not yet saved. In which case it would be called "Document1", etc., without a dot.
If InStr(ActiveDocument.Name,".") = 0 Then
'it is a new document, based on a template
ElseIf InStr(ActiveDocument.Name,".dotm") > 0 Then
'it is a/the template
This of course assumes that the ActiveDocument is the correct one. If they click a button in the document then this is correct, but if they use the Macros dialog then you may want to include additional checks.
I would use the following, which ignores differences in case (.dotm, .DOTm):
If InStr(UCase(ActiveDocument.Name), ".DOTM") > 0 Then
'it is a template..
Else
'it's just a document
End If
Checking ActiveDocument.AttachedTemplate.Name can also be useful, to confirm if the active-document is one based on your template.