0

我一直在网上搜索解决方案,但无济于事,我一直没有成功。

strSQL = "Update tTbl_LoginPermissions SET LoginName = '" & StrUserName & "', PWD = '" & StrPWD & "', fldPWDDate = '" & Now() & "'" & _
     "WHERE intLoginPermUserID = " & MyMSIDColumn0

一旦我得到错误,我想实际使用这个 where 子句:

'WHERE intLoginPermUserID IN (SELECT intCPIIUserID From vw_ADMIN_Frm_LoginBuilder)

这是整个代码:

Dim con As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim strSQL As String
    Const cSQLConn = "DRIVER=SQL Server;SERVER=dbswd0027;UID=Mickey01;PWD=Mouse02;DATABASE=Regulatory;"

Dim StrUserName As String, StrPWD As String

'passing variables
StrUserName = FindUserName()
StrPWD = EncryptKey(Me.TxtConPWD)

    'Declaring the SQL expression to be executed by the server
     strSQL = "Update tTbl_LoginPermissions SET LoginName = '" & StrUserName & "', PWD = '" & StrPWD & "', fldPWDDate = '" & Now() & "#" & _
     "WHERE intLoginPermUserID = " & MyMSIDColumn0
     'WHERE intLoginPermUserID = ANY (SELECT intCPIIUserID From vw_ADMIN_Frm_LoginBuilder)

     Debug.Print strSQL
    'connect to SQL Server
    Set con = New ADODB.Connection
    With con
        .ConnectionString = cSQLConn
        .Open
    End With

    'write back
    Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = con
        .CommandText = strSQL
        .CommandType = adCmdText
        .Execute
        Debug.Print strSQL
    End With

    'close connections
    con.Close
    Set cmd = Nothing
    Set con = Nothing

       MsgBox "You password has been set", vbInformation + vbOKOnly, "New Password"

最新代码生成错误:

 '/Declaring the SQL expression to be executed by the server
    strSQL = "Update dbo_tTbl_LoginPermissions " _
    & "SET LoginName = '" & StrUserName & "' " _
    & "SET PWD = '" & StrPWD & "' " _
    & "SET fldPWDDate = '" & Now() & "' " _
    & "WHERE intLoginPermUserID = 3;"

我已经去这个网站试图找出我的错误,但我仍然无法弄清楚:

4

3 回答 3

1

经过一番苦恼和帮助,事实证明FindUserName,使用 Win32API 函数并没有适当地修剪用户名。我将其更改为以下内容:

Public Function FindUserName() As String
    ' This procedure uses the Win32API function GetUserName
    ' to return the name of the user currently logged on to
    ' this machine. The Declare statement for the API function
    ' is located in the Declarations section of this module.

    Dim strBuffer As String
    Dim lngSize As Long

    strBuffer = Space$(255)
    lngSize = Len(strBuffer)

    If GetUserName(strBuffer, lngSize) = 1 Then
        FindUserName = Left$(strBuffer, lngSize - 1)
    Else
        FindUserName = "User Name not available"
    End If

 End Function

Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
于 2013-11-14T22:02:55.347 回答
0

试试这个:

strSQL = "Update tTbl_LoginPermissions SET LoginName = '" & replace(StrUserName, "'", "''") & "', PWD = '" & replace(StrPWD, "'", "''") & "', fldPWDDate = '" & Now() & "'" & _
 "WHERE intLoginPermUserID = " & MyMSIDColumn0
于 2013-11-06T22:12:53.353 回答
0

这不可避免地是纠正我的 (') 谜团的代码:

'/passing variables
StrUserName = FindUserName()
StrPWD = EncryptKey(Me.TxtConPWD)
StrUserId = Me.CboUser.Column(0)

 '/Declaring the SQL expression to be executed by the server
   strSQL = "Update tTbl_LoginPermissions SET " _
    & "LoginName = '" & StrUserName & "' , " _
    & "PWD = '" & StrPWD & "' ," _
    & "fldPWDDate = '" & Now() & "' " _
    & "WHERE intLoginPermUserID = '" & StrUserId & "'"
于 2013-12-11T16:24:36.427 回答