ADOTable ConnectionString

OK I've got my program finished, works perfect, now I want to use it on another laptop or PC.

The ConnectionString was setup as follows;

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Stephen\Desktop\HamLogbook\logbook1.mdb;Persist Security Info=False

When I try to use the program on a different laptop or PC the following error pops up;

"Could not find file "C:\Users\Stephen\Desktop\HamLogbook\logbook1.mdb"

So I thought ok, lets change the ConnectionString to;

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=logbook1.mdb;Persist Security Info=False

That way there was no folder setup and it would look in the same folder as the program, so I thought, still saying "Could not find file".

Looks like this is all I need now to finally put this baby to bed.

regards
Steve

Comments

  • I would save the file in MyDocuments folder and call win32 api SHGetFolderPath() to get the path to MyDocuments. I'm not a Delphi programmer, but here is how to do it in C language
    #include <windows.h>
    #include <iostream>
    #include <shlobj.h>
    
    #pragma comment(lib, "shell32.lib")
    
    int main() {
        CHAR my_documents[MAX_PATH];
        HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);
    
        if (result != S_OK)
            std::cout << "Error: " << result << "\n";
        else
            std::cout << "Path: " << my_documents << "\n";
        return 0;
    }
    
  • steve53 wrote:
    Looks like this is all I need now to finally put this baby to bed.
    Don't you believe it. That is the problem/beauty with writing your own software. You can always find something else to tweak.

    If you want the database in the MyDocuments folder as Ancient Dragon suggests, you could do it this way:
    uses
      SHFolder, shlobj;
    
    procedure Whatever(....);
    const
      CONNECTION_TEMPLATE = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%slogbook1.mdb;Persist Security Info=False';
    var
      MyDocs : array [0..MAX_PATH] of char;
      MyDocsWithSlash : string;
      hRes : HRESULT;
    begin
      hRes := SHGetFolderPath(0, CSIDL_PERSONAL, 0, SHGFP_TYPE_CURRENT, MyDocs);
      if hRes = S_OK then
      begin
        MyDocsWithSlash :=  IncludeTrailingPathDelimiter(string(MyDocs));
        ADOTable.ConnectionString := format(CONNECTION_TEMPLATE, [MyDocsWithSlash]);
      end
      else
        {Throw your rattle out of the pram};
    end;
    

    If you want the database in the same folder as your EXE you could do it this way:
    uses
      SHFolder, shlobj;
    
    procedure Whatever(....);
    const
      CONNECTION_TEMPLATE = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%slogbook1.mdb;Persist Security Info=False';
    var
      AppDir : string;
    begin
      AppDir := ExtractFilePath(Application.ExeName);
      AppDir :=  IncludeTrailingPathDelimiter(AppDir);
      ADOTable.ConnectionString := format(CONNECTION_TEMPLATE, [AppDir]);
    end;
    
  • Morning Guys, Maybe I got out of the wrong side of the bed this morning, I don't quit understand where to put the piece of code, if I want the database in the same folder as my EXE.

    I thought it would be in FormCreate, then I thought it would be in FormActivate. But they both come back with the same error;

    ADOTable1: Cannot perform this operation on an open dataset.

    regards

    Steve (confused)...(not that hard to get me confused these days...I need a break)
  • 2 things:
    1. You can put the code anywhere you like so long as it runs before you open the dataset.
    2. You can't set the table open in the object inspector at design time. That is, for a table you would put its Active property to FALSE in the object inspector. You must then open it (set it active) in code.
    Maybe like this:
    procedure Whatever(....);
    
    const
      CONNECTION_TEMPLATE = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%slogbook1.mdb;Persist Security Info=False';
    var
      AppDir : string;
    begin
      AppDir := ExtractFilePath(Application.ExeName);
      AppDir :=  IncludeTrailingPathDelimiter(AppDir);
      ADOTable.ConnectionString := format(CONNECTION_TEMPLATE, [AppDir]);
      [B]ADOTable.Active := TRUE;[/B]
    end;
    
    If you are using a TADOConnection then you would set its Connected property FALSE/TRUE in basically the same way.
  • Everything is so easy when you are told or shown how it's done and so obvious at times.

    I can have my well earned break now, without you guys I would be bold and senile by now, very much in your debt once again.

    very best wishes
    Steve
Sign In or Register to comment.