抱歉,這可能是一個愚蠢的問題和通用標題。但是,我一直在嘗試將登錄用戶表單中的值輸入到主模塊代碼中,該代碼生成一個 sql 查詢,該查詢會將資料從我們的總賬直接提取到 excel 中。
為了在輸入密碼時屏蔽密碼,我設定了一個非常簡單的用戶表單,它有兩個文本框(用戶名和密碼),一個登錄命令按鈕和一個取消按鈕。然后我對登錄單擊命令進行一些檢查,檢查用戶詳細資訊欄位是否為空(下載請求中有進一步的代碼將根據服務器的回應驗證用戶名是否正確)。
Whist 我無法通過在我的主模塊上,該模塊提供將生成查詢字串的類,以獲取輸入用戶表單的專案。我已經使用硬編碼的登錄詳細資訊進行了測驗,所以我知道代碼有效。我已經在我的用戶表單部分下面包含了與登錄單擊相關的部分以及圍繞此用戶表單呼叫的一般模塊部分,然后是類模塊的更新。主模塊由單擊作業表上的按鈕觸發。
用戶表單片段
Public Sub Login_Click()
'check user name is present
Dim corect_details As Integer
Dim uname As Integer
Dim pswrd As Integer
Dim nBIUsername As String
Dim nBIPassword As String
pswrd = 0
uname = 0
If Len(BIUsername) = 0 Then
MsgBox "please enter youre ISFE user name", vbOKOnly
Exit Sub
Else
uname = 1
nBIUsername = BIUsername
End If
If Len(BIPassword) = 0 Then
MsgBox "please enter a password", vbOKOnly
Exit Sub
Else
pswrd = 1
nBIPassword = BIPassword
End If
details = pswrd uname
If details = 2 Then
MsgBox "username is " & BIUsername & vbCrLf & "password is " & BIPassword
Me.Hide
Else
End If
End Sub
主要模塊片段
Dim nBIUsername As String
Dim nBIPassword As String
Sub showlogin()
Login.Show
GetDataFromBI
End Sub
Public Sub GetDataFromBI()
Dim BIReport As CBIReport: Set BIReport = New CBIReport
Load Login
With BIReport
.BIUsername = nBIUsername 'works when replaced with hardcoded username
.BIPassword = nBIPassword 'works when replaced with hardcoded Password
.REPORTPATH = MyReportPath
.ReportName = MyReportName
.FilterString = FilterString
.OutputOrigin = ThisWorkbook.Sheets("Output").Range("A1")
.GetData
If Not .IsLoginSuccessful Then MsgBox "Login not successful", vbCritical vbOKOnly: GoTo CleanExit
If InStr(.LastDownloadStatus, "Success") > 0 Then
MsgBox "Download successful", vbOKOnly
Else
MsgBox "Download not successful", vbCritical vbOKOnly
End If
End With
CleanExit:
Set BIReport = Nothing
End Sub
uj5u.com熱心網友回復:
為了使您的 2 個模塊級變數可用于用戶表單,請將它們宣告為Public而不是 using Dim:
Public nBIUsername As String
Public nBIPassword As String
您需要洗掉同名變數:
Dim nBIUsername As String
Dim nBIPassword As String
來自您的用戶表單,因為這些區域變數會影響公共變數。
您可以使用基于對話框的方法來回傳資料,而不是使用通常是不好的做法的全域變數/公共變數。
在模塊中創建一個型別來存盤用戶詳細資訊:
Public Type LoginDetals
Username As String
Password As String
IsValid As Boolean
End Type
添加退出按鈕并將用戶表單更改為:
Public Function getLogin() As LoginDetals
Me.Show vbModal
'// logic here
getLogin.Username = "bob"
getLogin.Password = "123"
getLogin.IsValid = True
End Function
Private Sub ExitButton_Click()
Unload Me
End Sub
然后在模塊中,您可以獲取詳細資訊:
Sub foo()
Dim userDetails As LoginDetals
userDetails = Login.getLogin()
If (userDetails.IsValid) Then
GetDataFromBI userDetails
End If
End Sub
Function GetDataFromBI(userDetails As LoginDetals)
MsgBox "hello " & userDetails.Username
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/492087.html
