2013-01-22 56 views
0

我一直在做這個......但一個變量不會顯示着工作了,爲什麼它的工作HTA有子,而在另一個子

我有一個下拉列表,選擇當它設置一個值,這是罰款它的工作...

但當我想要在另一個子例程中顯示它的值不會顯示?

請幫幫我嗎?

,如果你喜歡的任何代碼應寫入以某種方式讓我知道......我可是從互聯網這樣的IM不學習「訓練」正確....

<html> 
<head> 
<title>Break Logs</title> 

<HTA:APPLICATION 

icon=icon.ico 
    singleinstance="yes" 
border="thin" 
borderStyle="normal" 
caption="yes" 
maximizeButton="no" 
minimizeButton="no" 
showInTaskbar="yes" 
windowState="normal" 
innerBorder="yes" 
navigable="no" 
scroll="no" 
scrollFlat="no" 
sysMenu="yes" 
> 

</head> 

<SCRIPT Language="VBScript"> 

Window.ResizeTo 250, 250 

Dim objFSO, wshShell, strUser, Name, LastName, strLogs, strLogFile, strLocation 


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'  GETTING USERS NAME FROM AD THIS IM NOT USING WHEN AT HOME TESTING... 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
sub asdf 

Set wshShell = CreateObject("WScript.Shell") 
strUser = wshShell.ExpandEnvironmentStrings("%USERNAME%") 
Const ADS_SCOPE_SUBTREE = 2 
Set objRootDSE = GetObject("LDAP://RootDSE") 
strDomain = objRootDSE.Get("DefaultNamingContext") 
Set objConnection = CreateObject("ADODB.Connection") 
objConnection.Provider = "ADsDSOObject" 
objConnection.Open "Active Directory Provider" 
Set objCommand = CreateObject("ADODB.Command") 
Set objCommand.ActiveConnection = objConnection 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & strDomain & "'     WHERE objectCategory='User' AND samAccountName = '" & strUser & "'" 
Set objRecordSet = objCommand.Execute 
If Not objRecordSet.EOF Then 
strDN = objRecordSet.Fields("distinguishedName").Value 
End If 
Set MyUser = GetObject ("LDAP://" & strDN) 
Name = myUser.GivenName 
LastName = myUser.sn 

end sub 

Name = "Pavle" 
LastName = "Stojanovic" 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
'  SETTING TITLEBAR NAME 
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
document.title = "Break Logs For " & Name & " " & LastName 

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
CONST ForAppending = 8 

Set objFSO = CreateObject("Scripting.FileSystemObject") 

strLocation = "F:\" 
strLogFile = "Late Breaks.txt" 

If Not objFSO.FileExists(strLocation & strLogFile) Then 
    objFSO.CreateTextFile(strLocation & strLogFile) 
End If 



Sub Breaks 

MorningBreak = (" - Morning Break " & Name & " was late: " & _ 
     HowLate.Value & " Minutes" & vbCrLf & _ 
    " - Reason: " & Reason.Value & vbCrLF & _ 
    " - Date Entered: " & Now()) 


LunchBreak = (" - Lunch Break " & Name & " was late: " & _ 
     HowLate.Value & " Minutes" & vbCrLf & _ 
    " - Reason: " & Reason.Value & vbCrLF & _ 
    " - Date Entered: " & Now()) 

AfternoonBreak = (" - Afternoon Break " & Name & " was late: " & _ 
     HowLate.Value & " Minutes" & vbCrLf & _ 
    " - Reason: " & Reason.Value & vbCrLF & _ 
    " - Date Entered: " & Now()) 

RNA = (" - Ring No Answer Logged By " & Name & vbCrLF & _ 
    " - On Date: " & Now() & vbCrLF & _ 
    " - Reason: " & Reason.Value) 


Other = (" - Date Entered: " & Now() & vbCrLf & _ 
" - Reason: " & Reason.Value) 



If DropDown.Value = 1 Then 

' Nothing to be done in this option... 

ElseIf DropDown.Value = 2 Then 

BreakCode = MorningBreak 

ElseIf DropDown.Value = 3 Then 

BreakCode = LunchBreak 

ElseIf DropDown.Value = 4 Then 

BreakCode = AfternoonBreak 

ElseIf DropDown.Value = 5 Then 

BreakCode = RNA 

ElseIf DropDown.Value = 6 Then 

BreakCode = Other 

Else 

MsgBox "Something went wrong, this option shouldn't popup" 

End If 


' If I do MsgBox to display BreakCode it works here before leaving the sub ? 
End Sub 



Sub Submit 

MsgBox BreakCode ' this doesnt show up and below wont write to text ??? 

Set strLogs = objFSO.OpenTextFile(strLocation & strLogFile, ForAppending, True) 
    strLogs.WriteLine BreakCode & vbCrLf 
    strLogs.Close 

End Sub 



</SCRIPT> 
<body bgColor="LightSteelBlue"> 

<select size="1" name="DropDown" onChange="Breaks"> 
<option value="1">Choose Break</option> 
    <option value="2">Morning Break</option> 
    <option value="3">Lunch Break</option> 
    <option value="4">Afternoon Break</option> 
     <option value="5">Ring No Answer</option> 
     <option value="6">Other</option> 
     </select> 

<BR> 
<BR> 

Reason: 
<BR> 
<input type="text" name="Reason" size="10" style="width:200"> 

<BR> 
<BR> 

Minutes Late: 
<BR> 
<input type="text" name="HowLate" size="10"> 

<BR> 
<BR> 
<input type="button" value="Submit" onClick="Submit" title="Added Reason for being late."> 

</body> 
</html> 

回答

2

你不明確在任何地方定義變量BreakCode,所以會發生什麼情況是腳本引擎自動在它使用的範圍內定義變量。因此,您有兩個變量BreakCode:過程Breaks中的一個,以及過程Submit中的一個不同。後者從來沒有賦值,所以它仍然是空的。

插入線

Dim BreakCode 

此行之後:

Dim objFSO, wshShell, strUser, Name, LastName, strLogs, strLogFile, strLocation 

,問題就會消失。

+2

除了Ansgar,請您在VBScript代碼的第一行使用[Option Explicit](http://www.robvanderwoude.com/vbstech_debugging.php#OptionExplicit)。除了在每次運行沒有「明確選項」的腳本時小狗死亡之外,它都會爲您節省大量此類錯誤。 – AutomatedChaos

+0

謝謝夥伴!你一個好蛋! –

+0

所以我有另一個問題..我補充說,但價值的不來通過..對於HowLate.Value和Reason.Value? 我也使用選項顯式來聲明所有東西,如果這有所作爲? SORRY TO HASSLE YOU GUYS .. –

0

@Ansgar顯然不值得信任一個此代碼錯誤(+ 1)。如果你把它比作一個乾淨的 '解決方案':

HTA:

<html> 
<head> 
    <title>Break Logs</title> 
    <hta:application 
    id="breaks" 
    scroll="no" 
    ></hta> 
    <script type="text/vbscript" src="breaks.vbs"></script> 
</head> 
<body bgColor="LightSteelBlue"> 
    <form> 
    <select size="1" name="DropDown"> 
    <option value="1">Choose Break</option> 
    <option value="2">Morning Break</option> 
    <option value="3">Lunch Break</option> 
    <option value="4">Afternoon Break</option> 
    <option value="5">Ring No Answer</option> 
    <option value="6">Other</option> 
    </select> 
    <br /> 
    <br /> 
    Reason: 
    <br /> 
    <input type="text" name="Reason" size="10" style="width:200"> 
    <br /> 
    <br /> 
    Minutes Late: 
    <br /> 
    <input type="text" name="HowLate" size="10"> 
    <br /> 
    <br /> 
    <input type="button" value="Save" onClick="SaveIt" title="Added Reason for being late."> 
    </form> 
</body> 
</html> 

VBS:

Option Explicit 

Const ForAppending = 8 
Const csFSpec  = "wherever" 

Window.ResizeTo 250, 250 

Dim sFstName : sFstName = "Pavle" 
Dim sLstName : sLstName = "Stojanovic" 
document.title = "Break Logs For " & sFstName & " " & sLstName 

Sub SaveIt 
    Dim nOpt : nOpt = CInt(document.all.DropDown.Value) 
    If nOpt <> 1 Then 
    Dim sReason : sReason = Trim(document.all.Reason.Value) 
    Dim sHowLate : sHowLate = Trim(document.all.HowLate.Value) 
    Dim sBreak : sBreak = document.all.DropDown.options(nOpt - 1).text 
    If sReason <> "" And sHowLate <> "" Then 
     Dim sLog : sLog  = Join(Array(_ 
       "- " & sBreak & " " & sFstName & " was late" _ 
      , "- Minutes:  " & sHowLate _ 
      , "- Reason:  " & sReason _ 
      , "- Date Entered: " & Now() _ 
     ), vbCrLf) 
     MsgBox sLog 
     Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject") 
     Dim tsLogs : Set tsLogs = oFS.OpenTextFile(csFSpec, ForAppending, True) 
     tsLogs.WriteLine sLog 
     tsLogs.Close 
    Else 
     MsgBox "Fill in Reason and HowLate!" 
    End If 
    Else 
    MsgBox "Choose break type!" 
    End If 
End Sub 

您可以輕鬆地拿起別人:

  1. 錯誤的HTML:介於和之間,無(你應該使用驗證器like tidy)
  2. 提交表明提交給服務器;這對於.HTA
  3. 沒有任何意義。當DropDown選擇機會(通常是用戶稍後填充它們)時,選擇Reason和HowLate是無用的。事實上,DopDown可以很好地處理選擇變化
  4. 當用戶點擊Save時,會發生一切有趣的事情。所以,沒有必要在所有的全局變量
  5. OpenTextFile(,,True)將創建一個不存在的文件,所以檢查日誌文件的存在是多餘的脂肪

,您可以專注於核心缺陷:

  1. 你不能在生產中使用它,因爲只要你有多個用戶,他們在同一時間按Save和clobber日誌文件。
+0

謝謝!它看起來很漂亮,我LMAO .. –