Dot inside VBA string of an Access application gets interpreted as a number sign upon execution - vba

I'm working on a small Access database application that, among other things, exports a csv file when requested by the user.
The csv file should be created by the following code:
DoCmd.TransferText acExportDelim, "CSVexport", "tblExport", sLoc & Format(Date, "ddMMyyyy") & "_" & category & "_Export.csv", True
After fixing an issue with the data source part of the export (changed it from a query to a table), the _Export.csv part suddenly gets interpreted by Access as _Export#csv.
The error I'm getting:
Run-Time error '3011': The Microsoft Access database engine could not find the object '03022020_AluKippers_Export#csv'. Make sure the object exists and that you spell its name and path correctly . If '03022020_AluKippers_Export#csv' is not a local object, check your network connection or contact the server administrator.
I have no idea what's causing this.
Update: When changing the file extension to .txt, I get the same error. When using anything else after the dot, I get the cannot update error. I suspect the error may have something to do with the saved specification CSVexport...

Found the problem. The export specification I created, used the dot as the decimal symbol. After changing the decimal symbol back to , and the field delimiter from , to ;, the code in my question worked as expected.

Related

Check if file exists in OneDrive via VBA

I'm trying to check if an Excel file exists in OneDrive using Dir(Filename) (without opening it):
strFilePath = "https://d.docs.live.net/ce59asdf4hj34h5k/Folder 1/Folder 2/File.xlsm"
If Dir(strFilePath) <> "" Then
'do something
End If
but Dir(Filename) give me an error: Run-time error '52': Bad filename or number
I tried to do a workaround and check if the URL itself is valid (using the code sample from here) unfortunately the URL is always valid regardless if the file exists or not (if the file in question does not exist One Drive seems to return a default file, at least that's what it does if you test it in a browser, see screenshot below)
Any other ideas on how to solve the problem would be appreciated (I'm trying to do this without actually opening the file since this will create other problems I'm trying to avoid).
Note: I have OneDrive (Sync) installed, but using a local path unfortunately is out of solution's scope. The Soultion has to work with files that are not synced locally.

Object with special character "/" produces Automation Error in GetObject

We have an old Microsoft Access font end that serves as the GUI to our user database. I was never much of a VBA person so as I go through fixing bugs I'm learning as I go.
Our Access DB has a number of commands to sync info to Active Directory. One such command is to add a user to a group. However, whenever the group contains a / the group is never added.
The debug produces this as:
Run-time error -2147463168 (80005000)': Automation Error".
Printing out the targetgroup shows the DN as I expect it. Trying to escape the / before the GetObject doesn't help and causes its own auth error.
Here's the top part of the function -
Function AddGroup(TargetGroup, strUserID, Optional strOptReqBy)
Dim objDL
Set objUser = GetObject("LDAP://" & GetDName(CStr(strUserID)))
Set objDL = GetObject("LDAP://" & TargetGroup)
On Error Resume Next
objDL.Add (objUser.ADsPath)
objDL.SetInfo
On Error GoTo 0
This works fine if the group does not contain a /.
Debug points to Set objDL = GetObject("LDAP://" & TargetGroup)
Looking for some input on why this is happening. Thanks!
In an LDAP path, the / is a separator. Not only is the // used near the beginning, but you can also specify the server you want to connect to, followed by a /, then the DN of the object, like this:
LDAP://example.com/DC=example,DC=com
That's necessary if the computer you're running this from isn't not joined to the same (or trusted) domain than the domain you're connecting to.
So that means that if the DN of the object you want to bind to has a /, it will think that everything before the / is a server to connect to and it explodes.
So you just need to escape it, which, as you've already learned, is done with a \:
LDAP://OU=This\/That,DC=example,DC=com
So yeah, a simple replace will do:
Set objUser = GetObject("LDAP://" & Replace(GetDName(CStr(strUserID)), "/", "\/")
Don't feel bad. Even Microsoft has this bug in their code.

Access VBA Convert Linked Table to Local

I am running into a problem and can use some advice. I am linking to an excel spreadsheet and then i am trying to convert that link into a table.
my very basic function is as follows
dim mypath as string
docmd.transferspreadsheet aclink, acspreadsheettypeexcel112xml, "importeddata", mypath, true
runcommand acCmdConvertLinkedTableToLocal
this gives me a runtime error 2046
The command or action 'ComvertLinkedTableToLocal' isn't available now.
So, the reason for doing the link and then the conversion is when done manually, it will get rid of all the conversion errors that a normal import will do and then I have my basic VBA scripts which seem to run far quicker in Access than Excel.
Again, any help is duly duly appreciated
Groundhog.
I believe you need to have a specific linked table selected in order to run the acCmdConvertLinkedTableToLocal command. Try something like this after linking:
DoCmd.SelectObject acTable, "importeddata", True
DoCmd.RunCommand acCmdConvertLinkedTableToLocal
Alternatively, have you tried creating an empty table that matches the spreadsheet structure (i.e. using text fields instead of numeric fields) so that you don't get the import errors when you import instead of link?
Thank you for sharing this code example. I had to make one addition to get this to work correctly for me. I was getting a runtime message that the command was not available before adding a DoEvents after the SelectObject and prior to acCmdConvertLinkedTableToLocal.

UnauthorizedAccessException with File.AppendAllText in VB.NET

I have recently started getting System.UnauthorizedAccessException errors when using File.AppendAllText to write to a shared drive on the network. I think there were some changes to the network when this happened. The code in my application hasn't changed.
I have asked our IT dept to grant me full permission to the folder. I can see I have permissions for Modify, Read & Execute, Read, Write under my username if I navigate to the file and look at the Security tab under properties. I am also part of a group with read, write and modify permissions to the folder.
This works without error in the same folder:
File.WriteAllText(myFile, myText)
This generates a System.UnauthorizedAccessException error when it reaches the AppendallText:
If File.Exists(myFile) = False Then
' Create a file to write to.
Dim createText As String = logTime & " " & report_data
File.WriteAllText(myFile, createText)
Else
Dim appendText As String = logTime & " " & report_data
File.AppendAllText(myFile, appendText)
End If
I have tried deleting the file and creating it again, that made no difference.
I tried File.SetAttributes(myFile, FileAttributes.Normal)
The IT dept can't see what the problem is.
I can manually open, change and modify the file. The problem only arises if I am trying to do this programmatically.
Is there a different 'user' which tries to modify files? Could the file be open somehow, or would that generate a different error?
I'm using VB.NET 2012, .net framework 4.5, Windows 8.1
The network changes were the problem. It doesn't seem possible to resolve this as it is. Instead I made a copy of the text data, append my new text to that, delete the file, and save the updated text to a new file.

MS Access 2003 Error

I'm getting the following error when I try to run a macro that eventually uses the DoCmd.TransferText function. Here is the error below:
Cannot find object. Make sure the object exists and you spell its name and path correctly
I've used this method before to create a text file and add contents to it either from a table or a query.
Can someone correct me on this but doesn't the Transfer Text method create the file if it doesn't already exist?
Here is the code where i call it:
'Create Dispense File
DoCmd.TransferText acExportDelim, "DispenseExportSpec", "qryExport", DispenseFileName
Call Sleep(5000)
DoCmd.TransferText acExportDelim, "ExportDispenseCFSpec", "qryExportDispenseCF", ExportDispenseCFName
Call Sleep(5000)
(It fails on the first) This can't/shouldn't be a permissions issue as I have full read/write/edit abilities over the folder and its subfolders; it's not a path issue as I've verified multiple times, so what is the the problem here?
Access Version: MS Access 2003
Thanks
Try removing the spec argument and let it use the default? The documentation on that seems wonky. Just do a DoCmd.TransferText acExportDelim, , "qryExport", DispenseFileName