[英]Using access vba to pass the login via variables to automate the login to Sage Line 50 ver20
So i managed to connect Sage to my access database which is great but every time i close the database and open it up again i have to login again manually is there anyway of storing that data in Access?因此,我设法将 Sage 连接到我的 Access 数据库,这很棒,但是每次我关闭数据库并再次打开它时,我都必须再次手动登录,是否可以将这些数据存储在 Access 中? Be it via a variable or a table because clearly access can save the login information somewhere because i only need to login once.
无论是通过变量还是表,因为显然访问可以将登录信息保存在某处,因为我只需要登录一次。
Edit - added login connect details
编辑 - 添加登录连接详细信息
Sub login_Click()
Dim sagedb As Database
Dim accessdb As Database
Dim rs As Recordset
Dim strConnect As String
Set sagedb = OpenDatabase("Directory")
strConnect = "DSN=SageAccountsVer20;uid=xxx;pwd=xxx;"
Set accessdb = OpenDatabase("", False, False, strConnect)
accessdb.Close
Set accessdb = Nothing
Set rs = sagedb.OpenRecordset("dbo_authors")
Debug.Print rs(0)
Debug.Print "Recordset Opened Successfully"
rs.Close
sagedb.Close
Set rs = Nothing
I don't know Sage 50, but I've worked with the Sage 300 API, which is presumably similar;我不知道 Sage 50,但我使用过 Sage 300 API,这大概是相似的; you can/should store an instance of your Sage session and keep it alive for as long as you need the Sage connection alive.
您可以/应该存储 Sage 会话的实例,并在需要 Sage 连接时保持活动状态。
The session interface I've worked with needs to be initialized, and then opened;我使用过的会话接口需要初始化,然后打开; your code needs to provide a string handle, an appID, a program name and an app version to be initialized, and then when you open the session you need a userID, a password and a database name.
你的代码需要提供一个字符串句柄、一个appID、一个程序名和一个要初始化的应用程序版本,然后当你打开会话时你需要一个用户名、一个密码和一个数据库名。
Here's a C# wrapper interface I'm using (referenging Sage's COM API), that shows how the values are passed:这是我正在使用的 C# 包装器接口(参考 Sage 的 COM API),它显示了值是如何传递的:
public interface ISession : IDisposable
{
void Init(string handle, SageAppInfo info);
void Open(SageCredential credential, DateTime timestamp, int flags = 0);
IDbLink OpenDbLink(DBLinkType type, DBLinkFlags flags);
IEnumerable<string> Errors { get; }
}
public class SessionWrapper : ISession
{
private readonly ISessionComInterop _session;
public SessionWrapper(ISessionComInterop session)
{
_session = session;
}
public void Init(string handle, SageAppInfo info)
{
_session.Init(handle, info.AppId, info.ProgramName, info.AppVersion);
}
public void Open(SageCredential credential, DateTime timestamp, int flags = 0)
{
_session.Open(credential.UserId, credential.Password, credential.DatabaseName, timestamp, flags);
}
public IDbLink OpenDbLink(DBLinkType type, DBLinkFlags flags)
{
return new DbLinkWrapper(_session.OpenDBLink(type, flags));
}
public IEnumerable<string> Errors
{
get
{
for (var i = 0; i < _session.Errors.Count; i++)
{
yield return string.Format("[{0}] {1} ({2})", _session.Errors[i].Code, _session.Errors[i].Message, _session.Errors[i].Source);
}
}
}
public void Dispose()
{
_session.Dispose();
}
}
Of course a VBA implementation will be wildly different, but the important part is this:当然,VBA 实现会大不相同,但重要的部分是:
_session.Open(credential.UserId, credential.Password, credential.DatabaseName, timestamp, flags);
My calling code looks like this:我的调用代码如下所示:
_session.Init(string.Empty, _info);
_session.Open(_credential, DateTime.Now);
_db = _session.OpenDbLink(DBLinkType.Company, DBLinkFlags.ReadWrite);
So, have your code supply a UserId
a Password
, a DatabaseName
, a timestamp and whatever DBLinkFlags
you need;因此,让您的代码提供
UserId
、 Password
、 DatabaseName
、时间戳以及您需要的任何DBLinkFlags
; your VBA code could look like this:您的 VBA 代码可能如下所示:
mySession.Open APP_USERID, APP_PWD, APP_TESTDB, Now, DBLinkFlags.ReadWrite
Where mySession
is a global object variable, and then wherever APP_USERID
comes from is entirely up to you.其中
mySession
是一个全局对象变量,那么APP_USERID
来自何处完全取决于您。 Hard-code them in the .Open
call, make them in-code constants, or make them in-code variables and store the values in your Access database, or in an external xml configuration file, anything works;在
.Open
调用中对它们进行硬编码,使它们成为代码中的常量,或者使它们成为代码中的变量并将值存储在您的 Access 数据库或外部 xml 配置文件中,任何事情都可以; start by getting hard-coded credentials working, and then figure out a strategy to parameterize them.首先让硬编码凭证工作,然后找出一个策略来参数化它们。
This might explain why you're getting prompted each time for login info.这可能解释了为什么每次都会提示您输入登录信息。 I think you're using two opens - but only one is connected with your connxn string.
我认为您正在使用两个打开 - 但只有一个与您的 connxn 字符串连接。
Does this work any better?这工作更好吗?
Dim sagedb As Database
Dim rs As Recordset
Dim strConnect As String
strConnect = "DSN=SageAccountsVer20;uid=xxx;pwd=xxx;"
Set sagedb = OpenDatabase("Directory", False, False, strConnect)
Set rs = sagedb.OpenRecordset("dbo_authors")
Debug.Print rs(0)
Debug.Print "Recordset Opened Successfully"
rs.Close
sagedb .Close
Set sagedb = Nothing
sagedb.Close
Set rs = Nothing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.