Si. Se crea una nueva DBF con la estructura modificada y luego se le añaden los registros:
Code: Select all
function New( cAlias )
local oDlg, oGet, oBrw, oBtn, cTitle, cNewAlias, oBrwNew, lCopy := .F.
local cFieldName := Space( 10 ), cType := "Character", nLen := 10, nDec := 0
local aFields := { Array( 4 ) }, cDbfName := Space( 8 ), aTemp
local oLen, oDec, aType := { "Character", "Number", "Date", "Logical", "Memo" }
local bChange := {|| If( cType == "Character", (nLen := 10, nDec := 0, oDec:Disable() ),),;
If( cType == "Number", (nLen := 10, nDec := 0, oDec:Enable() ),),;
If( cType == "Date", (nLen := 8, nDec := 0, oDec:Disable() ),),;
If( cType == "Logical", (nLen := 1, nDec := 0, oDec:Disable() ),),;
If( cType == "Memo", (nLen := 10, nDec := 0, oDec:Disable() ),),;
oDlg:Update() }
local bEdit := {|| IF ( !Empty (aFields[1,1]) ,;
(oBtn:Enable (),;
cFieldName := aFields[oBrw:nArrayAt,1] ,;
cType := aFields[oBrw:nArrayAt,2] ,;
cType := aType[ aScan(aType, {|x| Left(x,1) = cType} )],;
Eval (bChange) ,;
nLen := aFields[oBrw:nArrayAt,3] ,;
nDec := aFields[oBrw:nArrayAt,4] ,;
oGet:SetPos( 0 ),;
oGet:SetFocus(),;
oDlg:Update() ) ,) ;
}
local bSave := {|| oBtn:Disable (),;
aFields[oBrw:nArrayAt,1] := cFieldname,;
aFields[oBrw:nArrayAt,2] := Left (cType,1),;
aFields[oBrw:nArrayAt,3] := nLen,;
aFields[oBrw:nArrayAt,4] := nDec,;
oBrw:SetArray (aFields),;
cFieldName := Space( 10 ),;
Eval (bChange) ,;
oDlg:Update() ,;
oGet:SetPos( 0 ),;
oGet:SetFocus(),;
oBrw:GoBottom();
}
if ! Empty( cAlias )
aFields = ( cAlias )->( DbStruct() )
cTitle = "Modify DBF struct"
else
cTitle = "DBF builder"
endif
DEFINE DIALOG oDlg TITLE cTitle SIZE 415, 400
@ 0.5, 2 SAY "Field Name" OF oDlg SIZE 40, 8
@ 0.5, 10 SAY "Type" OF oDlg SIZE 40, 8
@ 0.5, 17 SAY "Len" OF oDlg SIZE 40, 8
@ 0.5, 22 SAY "Dec" OF oDlg SIZE 20, 8
@ 1.4, 1 GET oGet VAR cFieldName PICTURE "!!!!!!!!!!" OF oDlg SIZE 41, 11 UPDATE
@ 1.3, 6.5 COMBOBOX cType ITEMS aType ;
OF oDlg ON CHANGE Eval (bChange) UPDATE
@ 1.4, 11.9 GET oLen VAR nLen PICTURE "999" OF oDlg SIZE 25, 11 UPDATE
@ 1.4, 15.4 GET oDec VAR nDec PICTURE "999" OF oDlg SIZE 25, 11 UPDATE
@ 0.9, 26 BUTTON "&Add" OF oDlg SIZE 45, 13 ;
ACTION AddField( @aFields, @cFieldName, @cType, @nLen, @nDec, oGet, oBrw )
@ 2.4, 26 BUTTON oBtn PROMPT "&Edit" OF oDlg SIZE 45, 13 ;
ACTION Eval (bSave)
@ 3.4, 26 BUTTON "&Delete" OF oDlg SIZE 45, 13 ;
ACTION DelField( @aFields, @cFieldName, oGet, oBrw )
@ 4.4, 26 BUTTON "Move &Up" OF oDlg SIZE 45, 13 ;
ACTION If( oBrw:nArrayAt > 1,;
( aTemp := aFields[ oBrw:nArrayAt ],;
aFields[ oBrw:nArrayAt ] := aFields[ oBrw:nArrayAt - 1 ],;
aFields[ oBrw:nArrayAt - 1 ] := aTemp,;
oBrw:GoUp() ),)
@ 5.4, 26 BUTTON "Move D&own" OF oDlg SIZE 45, 13 ;
ACTION If( oBrw:nArrayAt < Len( aFields ),;
( aTemp := aFields[ oBrw:nArrayAt ],;
aFields[ oBrw:nArrayAt ] := aFields[ oBrw:nArrayAt + 1 ],;
aFields[ oBrw:nArrayAt + 1 ] := aTemp,;
oBrw:GoDown() ),)
@ 6.4, 26 BUTTON "&Cancel" OF oDlg SIZE 45, 13 ;
ACTION oDlg:End()
@ 2.2, 2 SAY "Fields" OF oDlg SIZE 40, 8
@ 3.2, 1 XBROWSE oBrw ARRAY aFields AUTOCOLS ;
HEADERS "Name", "Type", "Len", "Dec" ;
COLSIZES 90, 55, 40, 40 ;
SIZE 140, 130 OF oDlg ;
ON DBLCLICK Eval (bEdit)
oBrw:nMarqueeStyle := MARQSTYLE_HIGHLROW
oBrw:bClrStd = { || If( oBrw:KeyNo() % 2 == 0, ;
{ CLR_BLACK, RGB( 198, 255, 198 ) }, ;
{ CLR_BLACK, RGB( 232, 255, 232 ) } ) }
oBrw:bClrSel = { || { CLR_WHITE, RGB( 0x33, 0x66, 0xCC ) } }
oBrw:SetColor( CLR_BLACK, RGB( 232, 255, 232 ) )
oBrw:CreateFromCode()
@ 12.1, 2 SAY "DBF Name:" OF oDlg SIZE 30, 8
if ! Empty( cAlias )
cDbfName = cGetNewAlias( cAlias )
endif
@ 14, 6 GET cDbfName PICTURE "!!!!!!!!!!!!" OF oDlg SIZE 100, 11
@ 10, 26 BUTTON If( Empty( cAlias ), "&Create", "&Save" ) OF oDlg SIZE 45, 13 ;
ACTION ( If( ! Empty( cDbfName ) .and. Len( aFields ) > 0,;
DbCreate( AllTrim( cDbfName ), aFields ),), oDlg:End(),;
lCopy := .T.,;
oBrwNew := Open( hb_CurDrive() + ":\" + CurDir() + "\" + AllTrim( cDbfName ) ) )
ACTIVATE DIALOG oDlg CENTERED ;
ON INIT ( Eval ( bChange ), oBtn:Disable() ) ;
VALID ! GETKEYSTATE( VK_ESCAPE )
if ! Empty( cAlias ) .and. lCopy
APPEND FROM ( cAlias )
oBrwNew:Refresh()
endif
return nil
La clave está en que si lCopy es .T. entonces: