可以把数据库的路径放在INI文件中
[DataBase]
DBpath=如果dbpath为空的话就弹出窗体指定数据库,否则直接连接

解决方案 »

  1.   

    可以在注册表里写一个键值用来存数据据路径.
    SaveSetting和GetSetting就可以进行读写注册表.
    写一个界面用来设置这个值就可以了.
      

  2.   

    SaveSetting("KX", "DatabaseUtilities", "Server", mvarServerName)
    mvarServerName = GetSetting("KX", "DatabaseUtilities", "Server", "")
    这两个函数的用法.
      

  3.   

    ....
    前面的代码你写,到这里时应该能选到一个路径存在一个字符串里.
    SaveSetting("yourname","Database","Path",strpath)
    完成了保存.
    可动态修改,以后你想改时也用上面的方法.
    ........
    使用时:
    strpath=GetSetting("yourname","Database","Path","")
    这就得到了以前你保存的路径了.
      

  4.   

    [HKEY_CURRENT_USER\Software\VB and VBA Program Settings]
    存在这个位置.
      

  5.   

    是否我只需设置savesetting()中的"yourname"与strpath这两项可以不管"database"和"path"这两个参数。
      

  6.   

    yourname,database,path 都是名字,随便起都行.
      

  7.   

    dim strconn as string
    strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" &  App.Path & "\aa.mdb"
    .......
    你能否把这个写完全。
      

  8.   

    INI文件保存在c:\test.ini
    [DB]
    path=程序:
    public Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" _
                    (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, _
                    ByVal lpRetunedString As String, ByVal nSize As Long, _
                    ByVal lpFileName As String) As Long
    public Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" _
                    (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
                    ByVal lplFileName As String) As LongPublic Function GetSetting(AppName As String, KeyName As String, FileName As String) As String
        '功能:读取INI文件
        Dim RetStr As String
        RetStr = String(255, Chr(0))
        GetSetting = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", _
                    RetStr, Len(RetStr), FileName))
    End Functionprivate sub Form1_Load()
      dim strPath as string
      
      strPath=GetSetting("DB", "path", "c:\test.ini")
      if strPath="" then
        指定路径的窗体.show
      else
        连接strPath下的数据库
      end if
    end sub指定路径的窗体代码:
    private sub CommandOK_Click()
      WritePrivateProfileString "DB", "Path", Text1.Text, "c:\test.ini"
    end sub
      

  9.   

    上面代码有一点问题,更改一下:
    private sub Form1_Load()
      dim strPath as string
      
      strPath=GetSetting("DB", "path", "c:\test.ini")
      if strPath="" then 
        指定路径的窗体.show vbmodal  ----这里弹出模式窗体
        strPath=指定的路径
      end if
      连接strPath下的数据库
    end sub指定路径的窗体代码:
    private sub CommandOK_Click()
      WritePrivateProfileString "DB", "Path", Text1.Text, "c:\test.ini"
      Unload Me
    end sub 
      

  10.   

    app.path表示当前目录呀
    数据库放在当前目录就不用写注册表
      

  11.   

    如果是这样,哪在程序安装完成之后,还必须写一个text.ini再将其拷入c:\;
      

  12.   

    晕,你可以改啊,我只是给你个例子
    将"c:\test.ini"改成app.path & "\test.ini"就可以了
      
      

  13.   

    tommychim;前面申明部分好象运行不起!
      

  14.   

    tommychim(大脚鸟):
    那你的方法不是还要动态找test.ini吗?还不如写到注册表里.
    internetibm():
    你都用APP.path了就是说库在当前程序所在路径了,那还要别的方法存起这个路径干吗?
      

  15.   

    写到注册表里不好,会形成注册表垃圾的,还是建议用INI比较好
    不用动态找,因为这个INI文件是你自己建立的,只是打包的时候要把它也打进去,安装的时候安装到程序所在的目录就可以了。前面的声明部分最好放在一个模块里,因为有两个窗体要调用。如果你放在一个窗体单元文件里,你要把public改为private
      

  16.   

    写错了;strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & "\aa.mdb"
    写入注册表与用app.path哪一个好一点。
      

  17.   

    to ljren_t(立志),internetibm():你们到底谁在问问题啊,我都糊涂了
      

  18.   

    strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & "\aa.mdb"----这里是什么,前面的路径呢???
      

  19.   

    如果库就存在当前程序所在路径那就用app.path吧.
    如果不在一块那就存到注册表里用.
      

  20.   

    对不起,我写错了,是这样的;
    dim strconn as string
    dim conn as new adodb.connection
    dim rs as new adodb.recordet
    strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =c:\aa.mdb"
    if conn.state=0 then conn.open strconn
    if rs.state=1 then rs.close
    现在如果要变路径
    一是写入注册表;
    二是用.ini文件;
    三是用app.path;
    哪现在如果用app.path应该怎样写呢?app.path好象也是一个变量。
    如果写成这样会出错:
    strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & app.path &"\aa.mdb"
    conn.open strconn
      

  21.   

    我前面给你的程序不是返回一个带有路径的strPath变量吗
    strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & strPath & "\aa.mdb"
      

  22.   

    我的老大,app.path指的是当前程序所在路径,如果aa.mdb在你的程序所在的路径才能用.