How does Trim differ between VBA and Windows Service(VB.Net)? - vba

I'm currently creating a Windows Service(VB.Net) using a VBA which already has been implemented many years ago. Some of the code I do not understand and would really appreciate if anyone can make it simpler to understand and explain.
strOrderNo = Trim(rs1("work_order") & "")
Here rs1 is a ADODB Recordset, I understand that part but I do not get what's done by & "" part.
Thank you!

Related

Running a SPSS-Syntax from excel using vba

I'm trying to run a SPSS-Syntax-command (as an example: "freq v1.") from an Excel-File using vba.
This used to work, but somehow not anymore. I have no idea why, I didn't change anything.
Here is the code:
text="freq v1."
Set objSpssApp = GetObject(, "Spss.Application")
Set objOutputDoc = objSpssApp.NewOutputDoc
objSpssApp.ExecuteCommands text, True
Has anyone an idea why this is not working anymore?
Thx a lot!
IIRC Spss.Application became Spss.Application16 some years ago. But you would require an SPSS license to use this.

Can you call a function as part of an SQL statement at the .execute line?

I have been tasked with doing some upgrades to a "data converter" that our companie uses to standardize client data. The "data converter" is from at least 2006, but could be much older. I have a fairly good understanding of VBA and some knowledge of SQL.
The actual conversion of data takes place in one very very long SQL statement (2,600 characters). It is built from many strings like the below. I am having trouble understanding the BreakName part, and was hoping someone could shed some light on it.
strSQLCompleteName = "BreakName(Trim([" & CStr(cboDBFieldFirstName) & "]),""" & txtNameDelimiter & """,3,1) AS UL_COMPLETE_NAME"
The BreakName part is a function in another module, but I wouldn't have thought you could call a function as part of a .Execute statement.
To answer my own question, you can call a sub/function from within an SQL statement. I got it working again, and then went line by line at the .execute line to see what happened.

VBA monitor folder for new files

So I'm trying to write a VBA program that will monitor a folder for new files and then do stuff with them. I've found some promising examples on using the WMI api:
Receive notification of file creation in VBA without polling
http://www.mrexcel.com/forum/excel-questions/211547-monitor-new-files-folder.html
https://blogs.technet.microsoft.com/heyscriptingguy/2004/10/11/how-can-i-automatically-run-a-script-any-time-a-file-is-added-to-a-folder/
But here's the thing: It seems like the tack everyone takes with these examples is to wire the VBA into an Excel spreadsheet as a macro. People treat Excel as a poor-man's programming environment. Fair enough. The problem is, I need this to run when the user is closed out out this magic excel file with the macro.
Something tells me I need to make a full windows application in visual studio with VB6.0 or C# and run the application in the background as some kind of a scheduled task. Is that the right path to take or is there something simple that I'm missing in these Excel/VBA tutorials?
(Apologies for the generality of the question. I know that the community appreciates specific questions.)
VBA and VBScript is similar. For WMI pretty much the same. Here are three scripts. You can also wire up WMI with event handlers so you can have multiple events rather than one as shown here.
VB6 is VBA that can be compiled into an exe. VB6 hosts the VBA language as does Office.
InstanceCreationEvent
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
InstanceModificationEvent
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceModificationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
InstanceDeletionEvent
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceDeletionEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""c:\\\\scripts""'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent
Wscript.Echo objLatestEvent.TargetInstance.PartComponent
Loop
I don't think Excel is a good solution for this kind of need. What about using VB.NET to do the work?
http://www.dreamincode.net/forums/topic/150149-using-filesystemwatcher-in-vbnet/
Yes, it's overkill, but if you get into it, you'll find all kinds of other really cool things that you can do with VB.NET. I love working with Excel, but I'm really a huge proponent of using the right tool for the job.
Ok- I found a solution for this. It's not pretty, but you can copy the blurb of code found at this tutorial:
https://msdn.microsoft.com/en-us/library/aa383665(v=VS.85).aspx
into an excel macro and it will work almost without modification, scheduling a task which will occur even after excel has closed down. The solution is to use VBA to grab an instance of the task scheduler
Set service = CreateObject("Schedule.Service")
call service.Connect()
And then perform all of the verbose configurations on that object to schedule the task. From there it's only a skip hop and a jump to write an other macro that will actually do the deed on the folder.

Sage50 ODBC VBA String Multiple Companies

I am looking for help with a vba connection for Sage 50. There are 3 different companies that have different directory paths for the driver SageLine50v20. I can connect normally through the ODBC by changing the path manually for each company but I have been unable to get this working in Access VBA.
I have tried the following connection string with no luck
stConnect = "ODBC;DRIVER=SageLine50v20" & ";UID=" & stUsername & ";PWD=" & stPassword & ";DIR=\\FOLDER\accounts\COMPANY.001\ACCDATA;"
I currently don't have access to this try anything else at the moment but I am looking for as many options/opinions as possible for when I do.
EDIT: Sorry that was a bit vague, I believe it was the ODBC call failed error.
If someone has a working vba example for pulling a table from sage 50 which includes the directory in the connection string that would be greatly appreciated.

Use toggle buttons to update a record in Access

Hopefully not too difficult question but I cannot figure this out and have been unable to find anything searching the forums.
I want to convert the output of my toggle boxes from 1,2,3,4,5 to the text each button displays.
Couldn't find any setting on the toggle boxes properties themselves so decided I would have to write a macro/vba to do it from the table but as it's quite new to me I am struggling on syntax.
I have tried doing this on the inbuilt data macro mainly, but also tried it via a query and vba and still couldn't figure it out.
[don't have any reputation yet so have not been allowed to post pics of my macro attmept]
please help! Any solution using vba, data macro or a query would be great
EDIT
To be specific rather than a message box I want to update field1 in my table "Major Equipment" based off the option group selection this is my latest attempt but still not sure how to reference the option group. Do I need to set grp equal to the option group and if so how? Is it something like forms!myform!optiongroup ?
Option Compare Database
Function MyFunc(grp As OptionGroup)
Dim rcrdset As Recordset
set grp =
Set rcrdset = Application.CurrentDb.OpenRecordset("Major Equipment", dbOpenDynaset)
Select Case grp.Value
Case 1
With rcrdset
.AddNew
![Field1] = "Not Reviewed"
Case 2
.AddNew
![Field1] = "Reviewed"
Case Else
MsgBox "Error"
End Select
End Function
Also just realised since these toggle buttons will be updated by the user and so I probably need an update rather than addnew?
http://i59.tinypic.com/2ym8wet.jpg
Your buttons are part of an Option Group. That is what you must reference. Below is a snippet from my net search.
From the AfterUpdate() event of your Option Group:
Call MyFunc(Me.MyGroup)
... which will use Select Case to evaluate:
Function MyFunc(grp As OptionGroup)
Select case grp.Value
Case 1
MsgBox "Option value is 1."
Case 2
MsgBox "Option value is 2."
Case Else
MsgBox "Huh?"
End Select
End Function
If you are entirely new to VBA, there are a half-dozen things to learn here, but they will serve you well. VBA provides a bit less-friendly-looking start than a macro, but I can tell you have more adventures ahead and I suggest you skip macros. For most needs, VBA will serve you better; and it's much easier to trouble shoot or provide details when you need advice.
To convert this to a useful function, you will fill a string variable rather than raising a message box. Then you can use the string variable to do something like run an update query. Your latest edit suggests you will go for something like:
strSQL = "UPDATE [Major Equipment] " _
& "SET Field1='" & strUserSelection & "' " _
& "WHERE MyID=" & lngThisRecord
DoCmd.RunSQL strSQL
Your last edit proposes using a DAO recordset. I think you might be fine with the humble DoCmd. Less to learn. You can hammer out a prototype of the query in good ol' Access; then switch to SQL View and paste the query into your VBA module. Insert variables as seen above, taking care with the quote marks. If it doesn't work, use Debug.Print to grab the value for strSQL and take that back to good ol' Access where you can poke at it into shape; use your findings to improve the VBA.