<%
'
'    Filename: fac_usrdatarecord1.asp
'    Generated with CodeCharge 2.0.5
'    ASP 2.0 & Templates.ccp build 11/30/2001
'

'-------------------------------
' 
' fac_usrdatarecord1 CustomIncludes Event begin
%>
<!-- #INCLUDE FILE="Common.asp" -->
<%
' fac_usrdatarecord1 CustomIncludes Event end


'===============================
' Save Page and File Name available into variables
'-------------------------------
sFileName = "fac_usrdatarecord1.asp"
sTemplateFileName = "fac_usrdatarecord1.html"
'===============================


'===============================
' fac_usrdatarecord1 PageSecurity begin
' fac_usrdatarecord1 PageSecurity end
'===============================

'===============================
' fac_usrdatarecord1 Open Event begin
' fac_usrdatarecord1 Open Event end
'===============================

'===============================
' fac_usrdatarecord1 OpenAnyPage Event begin
' fac_usrdatarecord1 OpenAnyPage Event end
'===============================

'===============================
'Save the name of the form and type of action into the variables
'-------------------------------
sAction = GetParam("FormAction")
sForm = GetParam("FormName")
'===============================

'-------------------------------
' fac_usrdatarecord1 CustomShowPage Event begin
'===============================
' Perform the form's action
'-------------------------------
' Initialize error variables
'-------------------------------
sUserDataRecordErr = ""

'-------------------------------
' Select the FormAction
'-------------------------------
Select Case sForm
  Case "UserDataRecord"
    UserDataRecordAction(sAction)
end select
'===============================

'===============================
' Display page
'-------------------------------
' Load HTML template for this page
'-------------------------------
LoadTemplate sAppPath & sTemplateFileName, "main"
'-------------------------------
' Load HTML template of Header and Footer
'-------------------------------
'-------------------------------
SetVar "FileName", sFileName


'-------------------------------
' Step through each form
'-------------------------------
UserDataRecord_Show
' Cancel if no parent key.
usrtab_key = GetParam("FAC_USRTAB_KEY")
if usrtab_key = "" then
  sAction = "cancel"
  UserDataRecordAction(sAction)
end if

'-------------------------------
' Process page templates
'-------------------------------
Parse "main", False
'-------------------------------
' Output the page to the browser
'-------------------------------
Response.write PrintVar("main")
' fac_usrdatarecord1 CustomShowPage Event end
'-------------------------------

'-------------------------------
' Destroy all object variables
'-------------------------------

' fac_usrdatarecord1 Close Event begin
' fac_usrdatarecord1 Close Event end

cn.Close
Set cn = Nothing
UnloadTemplate
'===============================
'===============================
' Action of the Record Form
'-------------------------------
Sub UserDataRecordAction(sAction)
'-------------------------------
' Initialize variables  
'-------------------------------
  Dim bExecSQL: bExecSQL = true
  Dim sActionFileName : sActionFileName = ""
  Dim sParams : sParams = "?"
  Dim sWhere : sWhere = "" 
  Dim bErr : bErr = False
  Dim pPKFAC_USRDATA_KEY : pPKFAC_USRDATA_KEY = ""
  Dim fldFAC_USRTAB_KEY : fldFAC_USRTAB_KEY = ""
  Dim fldFAC_USRDATA_CODE : fldFAC_USRDATA_CODE = ""
  Dim fldFAC_USRDATA_OMSCHR : fldFAC_USRDATA_OMSCHR = ""
  Dim fldFAC_USRDATA_VOLGNR : fldFAC_USRDATA_VOLGNR = ""
  Dim fldFAC_USRDATA_PRIJS : fldFAC_USRDATA_PRIJS = ""
  Dim fldFAC_USRDATA_VERVALDATUM : fldFAC_USRDATA_VERVALDATUM = ""
  Dim fldFAC_USRDATA_PARENTKEY : fldFAC_USRDATA_PARENTKEY = ""
'-------------------------------

'-------------------------------
' UserDataRecord Action begin
'-------------------------------
  sActionFileName = "fac_usrtabrecord1.asp"
  sParams = sParams & "FAC_USRTAB_KEY=" & ToURL(GetParam("Trn_FAC_USRTAB_KEY")) & "&"
  sParams = sParams & "FAC_USRDATA_OMSCHR=" & ToURL(GetParam("Trn_FAC_USRDATA_OMSCHR")) & "&"
  sParams = sParams & "sFAC_USRTAB_OMSCHRIJVING=" & ToURL(GetParam("Trn_sFAC_USRTAB_OMSCHRIJVING"))

'-------------------------------
' CANCEL action
'-------------------------------
  if sAction = "cancel" then

'-------------------------------
' UserDataRecord BeforeCancel Event begin
' UserDataRecord BeforeCancel Event end
'-------------------------------
    cn.Close
    Set cn = Nothing
    response.redirect sActionFileName & sParams
  end if
'-------------------------------

'-------------------------------
' Build WHERE statement
'-------------------------------
  if sAction = "update" or sAction = "delete" then
    pPKFAC_USRDATA_KEY = GetParam("PK_FAC_USRDATA_KEY")
    if IsEmpty(pPKFAC_USRDATA_KEY) then exit sub
    sWhere = "FAC_USRDATA_KEY=" & ToSQL(pPKFAC_USRDATA_KEY, "Number")
  end if
'-------------------------------


'-------------------------------
' Load all form fields into variables
'-------------------------------
  fldFAC_USRTAB_KEY = GetParam("FAC_USRTAB_KEY")
  fldFAC_USRDATA_CODE = GetParam("FAC_USRDATA_CODE")
  fldFAC_USRDATA_OMSCHR = GetParam("FAC_USRDATA_OMSCHR")
  fldFAC_USRDATA_VOLGNR = GetParam("FAC_USRDATA_VOLGNR")
  fldFAC_USRDATA_PRIJS = GetParam("FAC_USRDATA_PRIJS")
  fldFAC_USRDATA_VERVALDATUM = GetParam("FAC_USRDATA_VERVALDATUM")
  fldFAC_USRDATA_PARENTKEY = GetParam("FAC_USRDATA_PARENTKEY")

'-------------------------------
' Validate fields
'-------------------------------
  if sAction = "insert" or sAction = "update" then
    if IsEmpty(fldFAC_USRDATA_CODE) then
      sUserDataRecordErr = sUserDataRecordErr & "De waarde in veld Waarde code is verplicht.<br>"
    end if
    if not isNumeric(fldFAC_USRTAB_KEY) then
      sUserDataRecordErr = sUserDataRecordErr & "De waarde in veld  is niet correct.<br>"
    end if
    if not isNumeric(fldFAC_USRDATA_VOLGNR) then
      sUserDataRecordErr = sUserDataRecordErr & "De waarde in veld Volgnummer is niet correct.<br>"
    end if
    if not isNumeric(fldFAC_USRDATA_PRIJS) then
      sUserDataRecordErr = sUserDataRecordErr & "De waarde in veld Prijs is niet correct.<br>"
    end if
    if not isNumeric(fldFAC_USRDATA_PARENTKEY) then
      sUserDataRecordErr = sUserDataRecordErr & "De waarde in veld {lblWaarde} is niet correct.<br>"
    end if
'-------------------------------
' UserDataRecord Check Event begin
fldFAC_USRDATA_PRIJS = SafeToDouble(fldFAC_USRDATA_PRIJS)
cwhere = " fac_usrdata_upper = " & ToSQL(fldFAC_USRDATA_CODE, "Text") &_
         " AND fac_usrtab_key = " & ToSQL(fldFAC_USRTAB_KEY, "Number") &_
         " AND fac_usrdata_verwijder IS NULL " &_
         " AND fac_usrdata_key <> " & ToSQL(pPKFAC_USRDATA_KEY, "Number")

a = CInt(DLookUp("FAC_USRDATA", "fac_usrdata_key", cwhere))
if a <> 0 then
  sUserDataRecordErr = sUserDataRecordErr & "De waarde in veld Waarde code wordt al gebruikt.<br>"
end if
' UserDataRecord Check Event end
'-------------------------------
    If len(sUserDataRecordErr) > 0 then
      exit sub
    end if
  end if
'-------------------------------


'-------------------------------
' Create SQL statement
'-------------------------------
  select case sAction
    case "insert"
'-------------------------------
' UserDataRecord Insert Event begin
' Speciaal voor de toData
    sSQL = "insert into FAC_USRDATA (" & _
          "FAC_USRTAB_KEY," & _
          "FAC_USRDATA_CODE," & _
          "FAC_USRDATA_OMSCHR," & _
          "FAC_USRDATA_VOLGNR," & _
          "FAC_USRDATA_PRIJS," & _
          "FAC_USRDATA_VERVALDATUM," & _
          "FAC_USRDATA_PARENTKEY)" & _
          " values (" & _
          ToSQL(fldFAC_USRTAB_KEY, "Number") & "," & _
          ToSQL(fldFAC_USRDATA_CODE, "Text") & "," & _
          ToSQL(fldFAC_USRDATA_OMSCHR, "Text") & "," & _
          ToSQL(fldFAC_USRDATA_VOLGNR, "Number") & "," & _
          ToSQL(fldFAC_USRDATA_PRIJS, "Number") & "," & _
          toDate(fldFAC_USRDATA_VERVALDATUM) & "," & _
          ToSQL(fldFAC_USRDATA_PARENTKEY, "Number") & _
          ")"
' UserDataRecord Insert Event end
'-------------------------------
      if sSQL = "" then
      sSQL = "insert into FAC_USRDATA (" & _
          "FAC_USRTAB_KEY," & _
          "FAC_USRDATA_CODE," & _
          "FAC_USRDATA_OMSCHR," & _
          "FAC_USRDATA_VOLGNR," & _
          "FAC_USRDATA_PRIJS," & _
          "FAC_USRDATA_VERVALDATUM," & _
          "FAC_USRDATA_PARENTKEY)" & _
          " values (" & _
          ToSQL(fldFAC_USRTAB_KEY, "Number") & "," & _
          ToSQL(fldFAC_USRDATA_CODE, "Text") & "," & _
          ToSQL(fldFAC_USRDATA_OMSCHR, "Text") & "," & _
          ToSQL(fldFAC_USRDATA_VOLGNR, "Number") & "," & _
          ToSQL(fldFAC_USRDATA_PRIJS, "Number") & "," & _
          ToSQL(fldFAC_USRDATA_VERVALDATUM, "Date") & "," & _
          ToSQL(fldFAC_USRDATA_PARENTKEY, "Number") & _
          ")"
      end if
    case "update"
'-------------------------------
' UserDataRecord Update Event begin
'Speciaal voor de toDate
    sSQL = "update FAC_USRDATA set " & _
        "FAC_USRTAB_KEY=" & ToSQL(fldFAC_USRTAB_KEY, "Number") & _
        ",FAC_USRDATA_CODE=" & ToSQL(fldFAC_USRDATA_CODE, "Text") & _
        ",FAC_USRDATA_OMSCHR=" & ToSQL(fldFAC_USRDATA_OMSCHR, "Text") & _
        ",FAC_USRDATA_VOLGNR=" & ToSQL(fldFAC_USRDATA_VOLGNR, "Number") & _
        ",FAC_USRDATA_PRIJS=" & ToSQL(fldFAC_USRDATA_PRIJS, "Number") & _
        ",FAC_USRDATA_VERVALDATUM=" & ToDate(fldFAC_USRDATA_VERVALDATUM) & _
        ",FAC_USRDATA_PARENTKEY=" & ToSQL(fldFAC_USRDATA_PARENTKEY, "Number")
      sSQL = sSQL & " where " & sWhere
' UserDataRecord Update Event end
'-------------------------------
      if sSQL = "" then
      sSQL = "update FAC_USRDATA set " & _
        "FAC_USRTAB_KEY=" & ToSQL(fldFAC_USRTAB_KEY, "Number") & _
        ",FAC_USRDATA_CODE=" & ToSQL(fldFAC_USRDATA_CODE, "Text") & _
        ",FAC_USRDATA_OMSCHR=" & ToSQL(fldFAC_USRDATA_OMSCHR, "Text") & _
        ",FAC_USRDATA_VOLGNR=" & ToSQL(fldFAC_USRDATA_VOLGNR, "Number") & _
        ",FAC_USRDATA_PRIJS=" & ToSQL(fldFAC_USRDATA_PRIJS, "Number") & _
        ",FAC_USRDATA_VERVALDATUM=" & ToSQL(fldFAC_USRDATA_VERVALDATUM, "Date") & _
        ",FAC_USRDATA_PARENTKEY=" & ToSQL(fldFAC_USRDATA_PARENTKEY, "Number")
      sSQL = sSQL & " where " & sWhere
      end if
    case "delete"
'-------------------------------
' UserDataRecord Delete Event begin
dim tmpkey
tmpkey = "PK_"&strToTable("fac_usrdatarecord.asp")&"_KEY"
sSql = customFacDelete("fac_usrdatarecord.asp",getParam(tmpkey))
set tmpkey = Nothing
' UserDataRecord Delete Event end
'-------------------------------
      if sSQL = "" then
      sSQL = "delete from FAC_USRDATA where " & sWhere
      end if
  end select
'-------------------------------
'-------------------------------
' UserDataRecord BeforeExecute Event begin
' UserDataRecord BeforeExecute Event end
'-------------------------------

'-------------------------------
' Execute SQL statement
'-------------------------------
if len(sUserDataRecordErr) > 0 then Exit Sub
  on error resume next
  if bExecSQL then 
    cn.execute sSQL
  end if
  sUserDataRecordErr = ProcessError
  on error goto 0
  if len(sUserDataRecordErr) > 0 then Exit Sub
  cn.Close
  Set cn = Nothing
  response.redirect sActionFileName & sParams
'-------------------------------
' UserDataRecord Action end
'-------------------------------
end sub
'===============================

'===============================
' Display Record Form
'-------------------------------
Sub UserDataRecord_Show()
'-------------------------------
' UserDataRecord Show begin
'-------------------------------
  Dim sWhere : sWhere = ""
  Dim sFormTitle: sFormTitle = "Details tabelwaarde"
  Dim bPK : bPK = True
  Dim sFAC_USRDATA_PARENTKEYDisplayValue: sFAC_USRDATA_PARENTKEYDisplayValue = ""

'-------------------------------
' Load primary key and form parameters
'-------------------------------
  if sUserDataRecordErr = "" then
    fldFAC_USRTAB_KEY = GetParam("FAC_USRTAB_KEY")
    fldFAC_USRDATA_OMSCHR = GetParam("FAC_USRDATA_OMSCHR")
    SetVar "Trn_FAC_USRTAB_KEY", GetParam("FAC_USRTAB_KEY")
    SetVar "Trn_FAC_USRDATA_OMSCHR", GetParam("FAC_USRDATA_OMSCHR")
    SetVar "Trn_sFAC_USRTAB_OMSCHRIJVING", GetParam("sFAC_USRTAB_OMSCHRIJVING")
    pFAC_USRDATA_KEY = GetParam("FAC_USRDATA_KEY")
    SetVar "UserDataRecordError", ""
  else
    fldFAC_USRTAB_KEY = GetParam("FAC_USRTAB_KEY")
    fldFAC_USRDATA_CODE = GetParam("FAC_USRDATA_CODE")
    fldFAC_USRDATA_OMSCHR = GetParam("FAC_USRDATA_OMSCHR")
    fldFAC_USRDATA_VOLGNR = GetParam("FAC_USRDATA_VOLGNR")
    fldFAC_USRDATA_PRIJS = GetParam("FAC_USRDATA_PRIJS")
    fldFAC_USRDATA_VERVALDATUM = GetParam("FAC_USRDATA_VERVALDATUM")
    fldFAC_USRDATA_PARENTKEY = GetParam("FAC_USRDATA_PARENTKEY")
    SetVar "Trn_FAC_USRTAB_KEY", GetParam("Trn_FAC_USRTAB_KEY")
    SetVar "Trn_FAC_USRDATA_OMSCHR", GetParam("Trn_FAC_USRDATA_OMSCHR")
    SetVar "Trn_sFAC_USRTAB_OMSCHRIJVING", GetParam("Trn_sFAC_USRTAB_OMSCHRIJVING")
    pFAC_USRDATA_KEY = GetParam("PK_FAC_USRDATA_KEY")
    SetVar "sUserDataRecordErr", sUserDataRecordErr
    SetVar "FormTitle", sFormTitle
    Parse "UserDataRecordError", False
  end if
'-------------------------------

'-------------------------------
' Load all form fields

  fldfac_usrtab_naam = GetParam("fac_usrtab_naam")
'-------------------------------

'-------------------------------
' Build WHERE statement

  if IsEmpty(pFAC_USRDATA_KEY) then bPK = False
  
  sWhere = sWhere & "FAC_USRDATA_KEY=" & ToSQL(pFAC_USRDATA_KEY, "Number")
  SetVar "PK_FAC_USRDATA_KEY", pFAC_USRDATA_KEY
'-------------------------------
'-------------------------------
' UserDataRecord Open Event begin
pSql = "SELECT UP.fac_usrtab_naam " & _
       "FROM fac_usrtab UP " & _
       ", fac_usrtab UT " & _
       "WHERE UP.fac_usrtab_key = UT.fac_usrtab_parentkey " & _
       "AND ut.fac_usrtab_key = " & ToSQL(fldFAC_USRTAB_KEY, "Number")
openrs rs, pSql
waardeLabel = getValue(rs,"FAC_USRTAB_NAAM")
if waardeLabel = "" then waardeLabel = "Waarde referentie"

setVar "lblWaarde", FCLTLCL(waardeLabel)

koppel_waardeSql = "SELECT UD.FAC_USRDATA_KEY, UD.FAC_USRDATA_OMSCHR " & _
                   "FROM fac_usrdata UD " & _
                   ", fac_usrtab UT " & _
                   "WHERE UD.fac_usrtab_key = UT.fac_usrtab_parentkey " & _
                   "AND UD.fac_usrdata_verwijder IS NULL " & _
                   "AND UT.fac_usrtab_key = " & ToSQL(fldFAC_USRTAB_KEY, "Number") & _
                   " ORDER BY 2"
' UserDataRecord Open Event end
'-------------------------------

  SetVar "FormTitle", sFormTitle

'-------------------------------
' Build SQL statement and open recordset
'-------------------------------
  sSQL = "select * from FAC_USRDATA where " & sWhere
  openrs rs, sSQL
  bIsUpdateMode = (bPK and not(sAction = "insert" and sForm = "UserDataRecord") and not rs.eof)
'-------------------------------

'-------------------------------
' Load all fields into variables from recordset or input parameters
'-------------------------------
  if bIsUpdateMode then
    fldFAC_USRDATA_KEY = GetValue(rs, "FAC_USRDATA_KEY")
    fldFAC_USRTAB_KEY = GetValue(rs, "FAC_USRTAB_KEY")

'-------------------------------
' Load data from recordset when form displayed first time
'-------------------------------
    if sUserDataRecordErr = "" then
      fldFAC_USRDATA_CODE = GetValue(rs, "FAC_USRDATA_CODE")
      fldFAC_USRDATA_OMSCHR = GetValue(rs, "FAC_USRDATA_OMSCHR")
      fldFAC_USRDATA_VOLGNR = GetValue(rs, "FAC_USRDATA_VOLGNR")
      fldFAC_USRDATA_PRIJS = GetValue(rs, "FAC_USRDATA_PRIJS")
      fldFAC_USRDATA_VERVALDATUM = GetValue(rs, "FAC_USRDATA_VERVALDATUM")
      fldFAC_USRDATA_PARENTKEY = GetValue(rs, "FAC_USRDATA_PARENTKEY")
    end if
    SetVar "UserDataRecordInsert", ""
    Parse "UserDataRecordEdit", False
'-------------------------------
' UserDataRecord ShowEdit Event begin
' UserDataRecord ShowEdit Event end
'-------------------------------
  else
    if sUserDataRecordErr = "" then
      fldFAC_USRTAB_KEY = ToHTML(GetParam("FAC_USRTAB_KEY"))
      fldFAC_USRDATA_OMSCHR = ToHTML(GetParam("FAC_USRDATA_OMSCHR"))
    end if
    SetVar "UserDataRecordEdit", ""
    Parse "UserDataRecordInsert", False
'-------------------------------
' UserDataRecord ShowInsert Event begin
' UserDataRecord ShowInsert Event end
'-------------------------------
  end if
  Parse "UserDataRecordCancel", false
  if sUserDataRecordErr = "" then
'-------------------------------
' UserDataRecord Show Event begin
if fldFAC_USRDATA_VERVALDATUM <> "" then
  datum = CDate(fldFAC_USRDATA_VERVALDATUM)
  fldFAC_USRDATA_VERVALDATUM = Day(datum)&"-"&Month(datum)&"-"&Year(datum)
end if

if getParam("fac_usrtab_key") <> "" then
  fldFAC_USRTAB_KEY = getParam("fac_usrtab_key")
end if

pSql = "SELECT UT.fac_usrtab_naam " & _
       "FROM fac_usrtab UT " & _
       "WHERE UT.fac_usrtab_key = " & ToSQL(fldFAC_USRTAB_KEY, "Number")
openrs rs, pSql
fldfac_usrtab_naam = getValue(rs,"FAC_USRTAB_NAAM")
' UserDataRecord Show Event end
'-------------------------------
  end if

'-------------------------------
' Show form field
'-------------------------------
      SetVar "FAC_USRDATA_KEY", ToHTML(fldFAC_USRDATA_KEY)
      SetVar "FAC_USRTAB_KEY", ToHTML(fldFAC_USRTAB_KEY)
      SetVar "fac_usrtab_naam", ToHTML(fldfac_usrtab_naam)
      SetVar "FAC_USRDATA_CODE", ToHTML(fldFAC_USRDATA_CODE)
      SetVar "FAC_USRDATA_OMSCHR", ToHTML(fldFAC_USRDATA_OMSCHR)
      SetVar "FAC_USRDATA_VOLGNR", ToHTML(fldFAC_USRDATA_VOLGNR)
      SetVar "FAC_USRDATA_PRIJS", ToHTML(fldFAC_USRDATA_PRIJS)
      SetVar "FAC_USRDATA_VERVALDATUM", ToHTML(fldFAC_USRDATA_VERVALDATUM)
      SetVar "UserDataRecordLBFAC_USRDATA_PARENTKEY", ""
      SetVar "Selected", ""
      SetVar "ID", ""
      SetVar "Value", sFAC_USRDATA_PARENTKEYDisplayValue
      Parse "UserDataRecordLBFAC_USRDATA_PARENTKEY", True
      openrs rsFAC_USRDATA_PARENTKEY, ""&koppel_waardeSql&""
      while not rsFAC_USRDATA_PARENTKEY.EOF
        SetVar "ID", GetValue(rsFAC_USRDATA_PARENTKEY, 0) : SetVar "Value", GetValue(rsFAC_USRDATA_PARENTKEY, 1)
        if cstr(GetValue(rsFAC_USRDATA_PARENTKEY, 0)) = cstr(fldFAC_USRDATA_PARENTKEY) then SetVar "Selected", "SELECTED" else SetVar "Selected", ""
        Parse "UserDataRecordLBFAC_USRDATA_PARENTKEY", True
        rsFAC_USRDATA_PARENTKEY.MoveNext
      wend
      set rsFAC_USRDATA_PARENTKEY = nothing
    
  Parse "FormUserDataRecord", False

'-------------------------------
' UserDataRecord Close Event begin
' UserDataRecord Close Event end
'-------------------------------

Set rs = Nothing

'-------------------------------
' UserDataRecord Show end
'-------------------------------
End Sub
'===============================
%>