Set A Connection String In Excel To A Cell Value - excel

I'm creating a workbook for some folks to be able to manipulate some data, get blessings from all involved, then hand off to someone else to manually enter back into the system... Don't ask, we are afraid of scripting back to the system.
I am attempting to do this in the Data Connections Properties Window.
Data > Connections > Properties > Definition (I can't attach a screen shot cause I'm a noob I guess)
I have many different work arounds. I have even created a VB script that will do this, allow users to change server targets, pass different security credentials, but, just this last Monday there was some kind of office patch that screwed up all activeX and I had to go wipe out a temp file to correct it. I can't do that with a bunch of users that are less than tech savvy that are geographically separated, I don't have RDP authority.
For example, the connection string currently looks like:
DRIVER={iSeries Access ODBC Driver};
System=myserver.network.net;
I was hoping to set it to look like:
DRIVER={iSeries Access ODBC Driver};
System="Overview!D9";
The cell, Overview!D9, could then be changed by who ever to the correct system string, like myserver.network.net or server1.network.net.
Near as I can tell from my research, the connection properties window is pretty cut and dry and this might not be possible. But figured I would ask around.

Assuming System is just a string, then getting the value is simple.
' Assuming System is a String
Dim System As String
System = ThisWorkbook.Worksheets("Overview").Range("D9").Value
If System is part of another string, its much the same
' Get the value to use in the connection string
Dim System As String
System = ThisWorkbook.Worksheets("Overview").Range("D9").Value
Dim ConnectionString As String
ConnectionString = "DRIVER={iSeries Access ODBC Driver}; SYSTEM=" & System & ";"
If security is a problem, and your people generating/manipulating the data don't NEED the macros to do their work (ie, just the person entering the data back into the system does) then you don't need to put the macros in their workbooks.
You can add your macro's to the "Default Workbook" on the data entry person's computer. These macros will then be available to all workbooks they open on their computer.
Your data entry person gets the benefit of scripted automation and your users don't have to worry about email or anti-virus problems.

Related

Displaying Duplex Printing Option with Excel

I'm trying to print a bunch of tabs in Duplex in Excel. Ok, fun times.
This needs to be done in VBA, due to a lot of people needing this. More fun times.
Excel doesn't have Duplex printing as an option in VBA (But Access does for some reason).
You can access the option by going to File -> Print -> Page Setup -> Page -> Options -> Print on both sides. Hurray.
However, apparently this only changes one tab at a time, not all of them at once?
Is there a way to print a bunch of Excel workbook sheets in Duplex 3 mode programatically (Being accommodating for multiple printers)? It looks like no. So the next question is - is there a way to pop up the option described above with VBA, to at least make everything automated except the end users need to click on the right duplex setting a bunch?
Thank you
Alternatively, I might be able to change the duplex setting using the Access object model, but I'm stuck fairly early on trying to select the printer, let alone changing any settings.
Sub PrinterChangeWithAccess()
Dim AccessObject As Access.Application
Dim Printer_Name As Printer
Set AccessObject = CreateObject("Access.Application")
PrinterName = AccessObject.Dialogs(acDialogPrinterSetup).Show
End Sub
This type of problem - needing to use the Access object model from inside of Excel - is a great example of why something like the vba-excel tag should live, but that's a bit off topic....

Connect from Excel to Access database on internet server

I hope someone is able to help me out with the following. Currently I have an excel based tool that is able to retrieve and upload data (via ADO) to an Access database. This works perfectly fine, but now I need to use it on a server that is to be approached via Internet. Running the macro does not work any more where it is giving me a run time error: 'Your network access was interrupted'. I use the following connection string:
pad = ActiveWorkbook.Path
xdatabase = pad & "\Rolling forecast Database.accdb"
stDB = xdatabase
stConn = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & stDB & ";"
Although I see that people encounter the same problem I am not sure what it is that causes the macro not to function properly any more. It has to do with the fact that the server is probably not local? Any solutions for this problem?
Grateful for your swift response,
Cheers Michiel
You could use HiveLink (https://hivelink.io) to do this..
It would basically turn your spreadsheet into a live service and you would need a computer running all the time with have your original spreadsheet. HiveLink would help you create a "lightweight" spreadsheet that your users can use anywhere, which doesn't have any macros or calculations. A person using it would authenticate themselves, enter their input data, press a macro button and the inputs would be sent to your "master" spreadsheet. HiveLink would run the macro you choose in the master spreadsheet, which could be your Access database upload or whatever, then it would send back any results you want your user to have. The connection string would stay only in your master spreadsheet. End users don't need to install anything, but it only works in Excel (2010+) for Windows, and they would need to enable macros for it to work.
Thanks for your reaction guys. I solved it by using a local server! Cheers

Create a user comment field linked to read-only Oracle database

We have an application to track work orders (hundreds of thousands) built on an Oracle database. Data entry is cumbersome and report features non-existent. IT is inflexible. We do not get support from that end. Accordingly, users have created Excel "tools" to run queries and make sense of data using ADO or ODBC connections.
What we also need is a way to record comments on specific work order (WO) records and have those comments travel with those records somehow. There are multiple users using their own spreadsheets, all querying the same database.
I'm envisioning a junction-table approach, perhaps using Access, where some VBA could take a users comment from the row in the worksheet, capture the WO number, user id, date, and comment text and store it in an Access table. Those fields could then be retrieved by some more code. This would allow any user to see all comments by any other user related to a specific WO.
I'd greatly appreciate feedback ... on the practicality, preferable constructive, but brutally honest is ok too.
Much thanks,
Kevin
Apart from this being a horrible mess you could:
use SQL Server instead of access - you can get a free copy that will probably cater for your needs. Access will also work, but it is bodge. SQL server is more professional!
I would avoid doing data entry in excel. You could build a front end (possibly in Access with SQL Server backend) that allows data entry. You could add pretty simple code to all your workbooks, which will probably reference code that will be in an excel addin. The addin has code to simply open an access database, open a form and find the WO that the user was viewing in excel.
2a. Or you could use an excel addin with a data entry form, to do data entry, but beware managing the locking, refreshing and update of displayed data.
Creating excel addins is easy, (re-)distributing them is easy, access (or whatever front end you use) is designed to do record management (ie lock and update or lock and cancel or just view) etc.... Plus you want to avoid addinghte same or similar code to all your workbooks.
Each users workbook would have very simple code. Just to tell the addin what WO you want to operate on.
Do you have sharepoint? If so Access 2013 can deliver forms as webpages - very easily, so you might not even have to manage a front end access file.
Happier now?
For those that may stumble on this post, what we ended up doing was use VBA to store comments in a separate SQL database. Users double click the cell with the WO number to get an input form prompting for comments with options to add new, append or remove existing. Entries are passed to the SQL db and also to columns in the worksheet so users can see all the entries. Time stamp/Network ID provides when/who provided comment. Existing comments are fetched when users re-open file. Works great.

Excel crashes, VBA userform cannot save

I have a userform in Excel in which the user enters information and then hits an "add stock" button. Upon pressing this button, the information is entered into a spreadsheet and then the spreadsheet is saved with "ActiveWorkbook.Save".
The problem is that the work computers are old and Excel has a tendency to crash. When the spreadsheet is autorecovered, the add stock function no longer works, it crashes with a code 75 error. It seems that ActiveWorkbook.Save doesn't work in this case, until the user manually hits CTRL-S. The boss is adamant that our users are not computer savvy enough to manage this so I need to somehow check if Excel has crashed and if so automatically save the file before they start using it.
How would I check if we're in an autorecovery state, and then save it (without ActiveWorkbook.Save) so that the user can continue using the form without issues? Many thanks.
I can't find a direct way to check this, but here are a couple of kludgy options. It seems that if there are two states a workbook that an Autorecovered workbook can be in: Last saved by user or Autosaved. The caption reflects which state it's in. This function will check for the existence of that phrase in the caption.
Public Function IsInAutoRecoverMode(wb As Workbook)
Dim wn As Window
Set wn = wb.Windows(1)
IsInAutoRecoverMode = _
wn.Caption Like "*[Autosaved]?" Or _
wn.Caption Like "*[Last saved by user]?"
End Function
I'm not sure if there are more states that just this and this will certainly fail in non-English environments, so use with care.
When Last saved by user, the Workbook.Path property is where the file is stored. When Autosaved, the Workbook.Path property is equal to Application.AutoRecover.Path. Well, they're equal on my machine. That may just be a coincidence, but I doubt it.
Because the Last saved by user Path isn't distinctive, you can't use it to determine if you're in Autorecover mode. But if the user open the Autosaved workbook, you probably don't want to just Save, but rather Save As in the right location. You might need to use both these techniques to get the solution you want.
Make sure you tell your boss that these work-arounds because you're so industrious and inventive, but they are not documented and might fail at any time. At least, I wouldn't use them without understanding that they're based on some guesses. Let us know what you end up with.

Open Excel workbook as read-only via VB6

I have an application written in VB6 that writes data to a spreadsheet. I'm using the MS Excel 11.0 Object library to create an instance of Excel and open the book:
Dim xlApp As Excel.Application, remoteBook As Workbook
Set xlApp = New Excel.Application
Set remoteBook = xlApp.Workbooks.Open(sheetName)
In addition to writing to the workbook "sheetName", the program also allows the user to launch the workbook in order to view the cumulative results.
There is a chance, however slim it may be, that a user could have the workbook open for viewing the results while someone else is trying to write to it. I want to give the user writing to the sheet priority. Is there a way I can launch the sheet for viewing as read-only? There is a read-only property of the excel application object, but it is (of course) read-only.
How can I set up my program to write data to the workbook even if someone has accidentally left the file open at their desk?
Simply do this:
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
Where true is whether or not to open as Read Only. ReadOnly is the third parameter to this method.
I think you might be able to do it via the Workbook.ChangeFileAccess method as described here. Not sure if it will suit your circumstances though.
Let me make sure I have properly interpreted your issue:
Your app writes an excel file
The App launches the file in Excel
to the User
Now here's what I think you're saying:
Once the user is viewing the sheet, they may or may not want to edit that sheet.
In other words, you don't want to use
Set remoteBook = xlApp.Workbooks.Open( sheetName, , true)
100% of the time because the user viewing may want to change the data.
The downside is that this dastardly user may leave the file open to prevent other users from writing to that file.
Is that correct?
If so, it sounds like you may need to explicit state in your app to "Open for viewing" or "open for read-only" access and then toggle the Read Only property appropriately; which is probably undesirable.
However, you can't force a save on an office doc once someone else has it open.

Resources